2014-09-02 93 views
0

首先,我很抱歉,我知道我的問題將是非常基本的東西,但請我真的很感謝一些幫助,我承受了很大的壓力,所以做出了聰明的評論和存在諷刺並不會幫助從xml文件構建類

我有這段代碼: 至少我在嘗試。 我有這個xml文件,我需要將它的所有屬性保存在一個類中,這樣我只需要時就可以複製它。

我不知道該怎麼辦。

public static void firstFileXml(string sXml1) 
{ 
    var root = XElement.Load(@"sXml1"); 
    var controlElementsFirst = root.Descendants("books"); 
} 

的XML文件具有類似屬性:標籤,文本,LABEL_W等 我需要一個函數或somethign,讓我像進入: 探索(xmlLocation),並做休息。 因爲我需要做幾個XML文件

我需要建立一個類,讓我讀它。 假設我有XML的文件:

WAS

<books> 
     <book label='' page='' intro =''/> 
     <book label='' page='' intro =''/> 
     <book label='' page='' intro =''/> 
    </books> 

IS

<SHEET> 
<books> 
<book label='1' page='1' intro='1'/> 
<book label='2' page='2' intro='2'/> 
<book label='3' page='3' intro='3'/> 
</books> 
</SHEET> 

等。 我需要先讀這個XML文件,然後存儲在一個類的書屬性,這樣我就可以使用它的數百本書籍後

代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Xml.Serialization; 

static class Program 
{ 
    static void Main() 
    { 
     // get the xml as a string; note that we could also use 
     // Deserialize(Stream) to process a FileStream, but this 
     // works fine 
     string xml = File.ReadAllText(@"C:\Users\books.xml"); 
     var ser = new XmlSerializer(typeof(BookRoot)); 
     var root = (BookRoot)ser.Deserialize(new StringReader(xml)); 
     foreach (var book in root.Books) 
     { 
      Console.WriteLine("label: " + book.Label); 
      Console.WriteLine("page: " + book.Page); 
      Console.WriteLine("intro: " + book.Intro); 
      Console.ReadLine(); 
     } 
    } 
} 
[XmlRoot("SHEET")] 
public class BookRoot 
{ 
    private readonly List<Book> books = new List<Book>(); 
    [XmlArray("books"), XmlArrayItem("book")] 
    public List<Book> Books { get { return books; } } 
} 
public class Book 
{ 
    [XmlAttribute("label")] 
    public string Label { get; set; } 
    [XmlAttribute("page")] 
    public string Page { get; set; } 
    [XmlAttribute("intro")] 
    public string Intro { get; set; } 
} 
+1

您正在尋找的XML序列化 – 2014-09-02 07:55:38

+0

和反序列化 – Sayse 2014-09-02 07:56:46

+0

不是真的,因爲他們有節點而我主要使用屬性。請真正感謝一些示例算法或小代碼,只是有一個想法 – 2014-09-02 07:58:22

回答

4

在命令行:

xsd yourfile.xml 
xsd /c yourfile.xsd 

這將使用xml作爲模板(通過模式推理)創建C#類;然後你可以使用這些類與XmlSerializer,即

var ser = new XmlSerializer(typeof(YourRootType)); 
YourRootType root = (YourRootType)ser.Deserialize(source); 
Console.WriteLine(root.Foo); 
foreach(Bar bar in root.Bars) { 
    Console.WriteLine(bar.Name); 
    Console.WriteLine(bar.Id); 
} 

例如,具有:

<books> 
<book label='' page='' intro=''/> 
<book label='' page='' intro=''/> 
<book label='' page='' intro=''/> 
</books> 

此生成的模式:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="books" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
    <xs:element name="books" msdata:IsDataSet="true" msdata:Locale="en-US"> 
    <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="book"> 
      <xs:complexType> 
      <xs:attribute name="label" type="xs:string" /> 
      <xs:attribute name="page" type="xs:string" /> 
      <xs:attribute name="intro" type="xs:string" /> 
      </xs:complexType> 
     </xs:element> 
     </xs:choice> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

,然後將C#代碼:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.34014 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System.Xml.Serialization; 

// 
// This source code was auto-generated by xsd, Version=4.0.30319.18020. 
// 


/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class books { 

    private booksBook[] itemsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("book", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public booksBook[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class booksBook { 

    private string labelField; 

    private string pageField; 

    private string introField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string label { 
     get { 
      return this.labelField; 
     } 
     set { 
      this.labelField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string page { 
     get { 
      return this.pageField; 
     } 
     set { 
      this.pageField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string intro { 
     get { 
      return this.introField; 
     } 
     set { 
      this.introField = value; 
     } 
    } 
} 

XmlSerializer工作正常,並會cor rectly反序列化的數據,例如:

var ser = new XmlSerializer(typeof(books)); 
var root = (books)ser.Deserialize(new StringReader(xml)); 
foreach(var book in root.Items) 
{ 
    Console.WriteLine("label: " + book.label); 
    Console.WriteLine("page: " + book.page); 
    Console.WriteLine("intro: " + book.intro); 
} 

需要注意的是,你可以用手輕鬆了不少做:

[XmlRoot("books")] 
public class BookRoot { 
    private readonly List<Book> books = new List<Book>(); 
    [XmlElement("book")] 
    public List<Book> Books { get { return books; } } 
} 
public class Book { 
    [XmlAttribute("label")] 
    public string Label {get;set;} 
    [XmlAttribute("page")] 
    public string Page {get;set;} 
    [XmlAttribute("intro")] 
    public string Intro {get;set;} 
} 

例如:

var ser = new XmlSerializer(typeof(BookRoot)); 
var root = (BookRoot)ser.Deserialize(new StringReader(xml)); 
foreach(var book in root.Books) 
{ 
    Console.WriteLine("label: " + book.Label); 
    Console.WriteLine("page: " + book.Page); 
    Console.WriteLine("intro: " + book.Intro); 
} 

用最最近的編輯爲XML添加了一個級別,這些屬性需要稍微調整一下 - 更改根元素名稱,並添加一個額外的級別(books/book =>SHEET/books/book):

[XmlRoot("SHEET")] 
public class BookRoot 
{ 
    private readonly List<Book> books = new List<Book>(); 
    [XmlArray("books"), XmlArrayItem("book")] 
    public List<Book> Books { get { return books; } } 
} 
+0

謝謝爲了您的幫助,您可以看看我的XML結構嗎?此外,我正在對視覺工作室 – 2014-09-02 08:00:06

+0

@AniAni你的xml實際上不是xml,但我將編輯它基於它的東西。 xsd.exe工具是.NET sdk免費提供的一部分 – 2014-09-02 08:01:48

+0

謝謝,但是如果它不是一個工具,對我來說會更好,我希望我能理解它,但對我來說很困難 – 2014-09-02 08:02:39