Linq到XML数据结构

时间:2015-05-25 19:55:08

标签: c# xml linq linq-to-xml

这是我的XML:

<home>
  <contents>
    <row>
        <content>
            <idContent>1</idContent>
            <title>title1</title>
        </content>
        <content>
            <idContent>2</idContent>
            <title>title2</title>
        </content>
    </row>
    <row>
        <content>
            <idContent>3</idContent>
            <title>title3</title>
        </content>
        <content>
            <idContent>4</idContent>
            <title>title4</title>
        </content>
    </row>
</contents>

我想将此信息存储在对象列表中

列出myList = ...

内容可以是:

int idContent;
string title;
int row_number;

每个Content对象都必须将行存储在XML中。

这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:1)

假设row_number只是与XML中出现的顺序相关的序列,那么你可以这样做:

var doc = XDocument.Parse(xml);

var contents = doc.Descendants("row")
    .Select((e, index) => new {Row = e, RowIndex = index})
    .SelectMany(x => x.Row.Elements("content").Select(e => new {Content = e, x.RowIndex}))
    .Select(x => new Content
    {
        IdContent = (int)x.Content.Element("idContent"),
        Title = (string)x.Content.Element("title"),
        RowNumber = x.RowIndex + 1
    }).ToList();

答案 1 :(得分:0)

我能想到的最好的解决方案 - 虽然它没有使用Linq到XML - 是将XML文档提供给其中一个不同的XML Schema生成器,如http://www.freeformatter.com/xsd-generator.html,并将生成的Schema管道直接输入xsd.exe (见https://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx)。生成的代码可用于读取(和写入)您提供的XML文档,您当然可以将Linq应用于此集合上的对象。

但是,由于您提到了尚未出现在示例XML中的row_number字段,因此您必须将其添加到XML中,或者之后手动编辑XML Schema。

答案 2 :(得分:0)

我将此方法用于类似的场景:

public static Object CreateObject(string XMLString, Object YourClassObject)
{
     System.Xml.Serialization.XmlSerializer oXmlSerializer = new System.Xml.Serialization.XmlSerializer(YourClassObject.GetType());
     //The StringReader will be the stream holder for the existing XML file 
     YourClassObject = oXmlSerializer.Deserialize(new System.IO.StringReader(XMLString));
     //initially deserialized, the data is represented by an object without a defined type 
     return YourClassObject;
}

使用此方法,您可以从XML String创建类对象。我还没有在您的方案中对其进行测试,但您可以将其用于关注home类的对象:

public class home
{
    public List<row> contents;
}
public class row
{
    public List<content> content;
}
public class content
{
    public int idContent;
    public string title;
}

<强> USAGE:

home h = new home();
h = (home)CreateObject(xml, h);

请记住,变量和类名必须与XML节点完全相同。

<强> EXTRA:

如果要将类对象转换为XML String,请使用以下方法:

string CreateXML(Object YourClassObject)
{
     XmlDocument xmlDoc = new XmlDocument();   //Represents an XML document, 
     // Initializes a new instance of the XmlDocument class.          
     XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());
     // Creates a stream whose backing store is memory. 
     using (MemoryStream xmlStream = new MemoryStream())
     {
         xmlSerializer.Serialize(xmlStream, YourClassObject);
         xmlStream.Position = 0;
         //Loads the XML document from the specified string.
         xmlDoc.Load(xmlStream);
         return xmlDoc.InnerXml;
     }
}