2014-09-02 68 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