Is this the most efficient way of loading xml values into a structure?

跟風遠走 提交于 2019-12-11 08:35:10

问题


I am trying to load data from XML string into a structure of some sort so once loaded I can then say Data.PropertyName to read the values.

Is the below code the most optimal way of loading the data into the structure?

Obviously calling First() has a memory hit so if you have elements with sub elements will calling First() for each one become an issue?

Thanks

    string xml = @"<ROOT>
                   <ID>1</ID>
                   <NAME>RF1</NAME>
                   <STAT>10200</STAT>
                   <TEST>
                       <ID>1</ID>
                       <NAME>BIGUN</NAME>
                   </TEST>
                   </ROOT>
                   ";

     XElement Data = XElement.Parse(xml);


     var Element = (from p in Data.Descendants("ROOT") 
                   select new { 
                        ID = (int)p.Element("ID"), 
                        Test = new { 
                                    ID = p.Element("TEST").Descendants("ID").First(), 
                                    NAME = p.Element("TEST").Descendants("NAME").First() 
                                 }, 
                        Stat = p.Element("STAT") }).First();

//Read Element.ID, Element.Test.Name

回答1:


You could use XmlSerializer to deserialize it?

using System;
using System.IO;
using System.Xml.Serialization;

[XmlRoot("ROOT")]
public class MyType
{
    [XmlElement("ID")]
    public string Id { get; set; }
    [XmlElement("NAME")]
    public string Name { get; set; }
    [XmlElement("STAT")]
    public string Stat { get; set; }
    [XmlElement("TEST")]
    public MyOtherType Nested { get; set; }
}
public class MyOtherType
{
    [XmlElement("ID")]
    public string Id { get; set; }
    [XmlElement("NAME")]
    public string Name { get; set; }
}
static class Program
{

    static void Main()
    {
        string xml = @"<ROOT>
                   <ID>1</ID>
                   <NAME>RF1</NAME>
                   <STAT>10200</STAT>
                   <TEST>
                       <ID>1</ID>
                       <NAME>BIGUN</NAME>
                   </TEST>
                   </ROOT>";
        MyType obj = (MyType) new XmlSerializer(typeof(MyType))
            .Deserialize(new StringReader(xml));
        Console.WriteLine(obj.Id);
        Console.WriteLine(obj.Name);
        Console.WriteLine(obj.Stat);
        Console.WriteLine(obj.Nested.Id);
        Console.WriteLine(obj.Nested.Name);
    }
}



回答2:


I'd go for deserialization and Xml attributes: much more readable.




回答3:


No, it's not the most optimal way, but it's likely to be good enough.

The most optimal for performance would be to write your own XML parser that is specialised for just this, but that would of course be a lot more code.

I don't know why you are concerned with the First method. It will just let the first item through and then stop reading, so it won't cause any problems. The XML parser still has to parse all the children, of course.



来源:https://stackoverflow.com/questions/1531150/is-this-the-most-efficient-way-of-loading-xml-values-into-a-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!