简体   繁体   English

这是将xml值加载到结构中的最有效方法吗?

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

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. 我正在尝试将XML字符串中的数据加载到某种结构中,因此一旦加载,我就可以说Data.PropertyName来读取值。

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? 显然调用First()有一个内存命中,所以如果你有子元素的元素将调用First()为每个成为一个问题?

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

You could use XmlSerializer to deserialize it? 您可以使用XmlSerializer进行反序列化吗?

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);
    }
}

我会去反序列化和Xml属性:更具可读性。

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. 性能最优化的是编写专门用于此的XML解析器,但这当然会包含更多代码。

I don't know why you are concerned with the First method. 我不知道为什么你关心First一种方法。 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. 当然,XML解析器仍然需要解析所有子节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM