C#XML序列化非嵌套数组/列表
我正在编写一个C#C#XML序列化非嵌套数组/列表,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我正在编写一个C#WinForm应用程序,它应该读取一个预先存在的XML文件。 我需要解析XML文件,并建立一个数据结构来反映XML文件内容。 我在XML序列化方面有一些经验,因此我决定使用.NETXML.serialization功能。 我陷入了一个奇怪的XML结构中,我无法在类中匹配它(通过属性、元素等): 我还有一个次要问题。反映此结构的最佳类层次结构是: a) 以与xml元素相同的方式嵌套类 b) 将所有类保持在同一级别(没有层次结构)? 有一些副作用需要考虑?您可以如下定义C对象 [X
WinForm
应用程序,它应该读取一个预先存在的XML
文件。
我需要解析XML
文件,并建立一个数据结构来反映XML
文件内容。
我在XML
序列化方面有一些经验,因此我决定使用.NET
XML.serialization
功能。
我陷入了一个奇怪的XML
结构中,我无法在类中匹配它(通过属性、元素等):
我还有一个次要问题。反映此结构的最佳类层次结构是:
a) 以与xml元素相同的方式嵌套类
b) 将所有类保持在同一级别(没有层次结构)?
有一些副作用需要考虑?您可以如下定义
C
对象
[XmlRoot(ElementName="comment")]
public class Comment {
[XmlAttribute(AttributeName="number")]
public string Number { get; set; }
[XmlAttribute(AttributeName="value")]
public string Value { get; set; }
}
[XmlRoot(ElementName="title_block")]
public class Title_block {
[XmlElement(ElementName="title")]
public string Title { get; set; }
[XmlElement(ElementName="company")]
public string Company { get; set; }
[XmlElement(ElementName="rev")]
public string Rev { get; set; }
[XmlElement(ElementName="date")]
public string Date { get; set; }
[XmlElement(ElementName="source")]
public string Source { get; set; }
[XmlElement(ElementName="comment")]
public List<Comment> Comment { get; set; }
}
[XmlRoot(ElementName="sheet")]
public class Sheet {
[XmlElement(ElementName="title_block")]
public Title_block Title_block { get; set; }
[XmlAttribute(AttributeName="number")]
public string Number { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="tstamps")]
public string Tstamps { get; set; }
}
选中此项您可以按如下方式定义
C
对象
[XmlRoot(ElementName="comment")]
public class Comment {
[XmlAttribute(AttributeName="number")]
public string Number { get; set; }
[XmlAttribute(AttributeName="value")]
public string Value { get; set; }
}
[XmlRoot(ElementName="title_block")]
public class Title_block {
[XmlElement(ElementName="title")]
public string Title { get; set; }
[XmlElement(ElementName="company")]
public string Company { get; set; }
[XmlElement(ElementName="rev")]
public string Rev { get; set; }
[XmlElement(ElementName="date")]
public string Date { get; set; }
[XmlElement(ElementName="source")]
public string Source { get; set; }
[XmlElement(ElementName="comment")]
public List<Comment> Comment { get; set; }
}
[XmlRoot(ElementName="sheet")]
public class Sheet {
[XmlElement(ElementName="title_block")]
public Title_block Title_block { get; set; }
[XmlAttribute(AttributeName="number")]
public string Number { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="tstamps")]
public string Tstamps { get; set; }
}
检查此项尝试此项
使用
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
班级
[XmlRoot(ElementName = "comment")]
public class Comment
{
[XmlAttribute(AttributeName = "number")]
public string Number { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "title_block")]
public class Title_block
{
[XmlElement(ElementName = "title")]
public string Title { get; set; }
[XmlElement(ElementName = "company")]
public string Company { get; set; }
[XmlElement(ElementName = "rev")]
public string Rev { get; set; }
[XmlElement(ElementName = "date")]
public string Date { get; set; }
[XmlElement(ElementName = "source")]
public string Source { get; set; }
[XmlElement(ElementName = "comment")]
public List<Comment> Comment { get; set; }
}
[XmlRoot(ElementName = "sheet")]
public class Sheet
{
[XmlElement(ElementName = "title_block")]
public Title_block Title_block { get; set; }
[XmlAttribute(AttributeName = "number")]
public string Number { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "tstamps")]
public string Tstamps { get; set; }
}
这段代码从一个文件(在application*.exe文件夹中称为xml.xml)中读取xml,然后将其重新序列化为一个名为dezerializedXML的对象 试试这个
使用
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
班级
[XmlRoot(ElementName = "comment")]
public class Comment
{
[XmlAttribute(AttributeName = "number")]
public string Number { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "title_block")]
public class Title_block
{
[XmlElement(ElementName = "title")]
public string Title { get; set; }
[XmlElement(ElementName = "company")]
public string Company { get; set; }
[XmlElement(ElementName = "rev")]
public string Rev { get; set; }
[XmlElement(ElementName = "date")]
public string Date { get; set; }
[XmlElement(ElementName = "source")]
public string Source { get; set; }
[XmlElement(ElementName = "comment")]
public List<Comment> Comment { get; set; }
}
[XmlRoot(ElementName = "sheet")]
public class Sheet
{
[XmlElement(ElementName = "title_block")]
public Title_block Title_block { get; set; }
[XmlAttribute(AttributeName = "number")]
public string Number { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "tstamps")]
public string Tstamps { get; set; }
}
这段代码从一个文件(在application*.exe文件夹中称为xml.xml)中读取xml,然后将其重新序列化为一个名为dezerializedXML的对象 我们希望你的问题已经解决。如果没有,也许可以考虑更新你的问题。。。。我们出去度假了,我马上去看看。我们希望你的问题得到解决。如果没有,也许可以考虑更新你的问题。。。。我出去度假了,我很快就去看看
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("xml.xml");
string XML = xmlDoc.InnerXml.ToString();
byte[] BUFXML = ASCIIEncoding.UTF8.GetBytes(XML);
MemoryStream ms1 = new MemoryStream(BUFXML);
XmlSerializer DeserializerPlaces = new XmlSerializer(typeof(Sheet));
using (XmlReader reader = new XmlTextReader(ms1))
{
Sheet dezerializedXML = (Sheet)DeserializerPlaces.Deserialize(reader);
}// Put a break-point here, then mouse-over dezerializedXML and you should have you values
}
catch (System.Exception)
{
throw;
}