这是将XML值加载到结构中的最有效方法吗?[英] Is this the most efficient way of loading xml values into a structure?

本文是小编为大家收集整理的关于这是将XML值加载到结构中的最有效方法吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图将数据从XML字符串加载到某种结构中,因此一旦加载,我就可以说data.propertyname读取值.

以下代码是将数据加载到结构中的最佳方法吗?

显然调用first()有记忆命中,因此,如果您有带有子元素的元素,则每个元素都会为每个元素调用()成为问题吗?

谢谢

    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

推荐答案

您可以使用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属性:更可读性.

其他推荐答案

不,这不是最佳的方法,但可能足够好.

最佳性能是编写自己的XML解析器,该解析器专门为此而言,但这当然是更多的代码.

我不知道您为什么关心First方法.它只会让第一项通过然后停止阅读,因此不会引起任何问题.当然,XML解析器仍然必须解析所有孩子.

本文地址:https://www.itbaoku.cn/post/1557110.html

问题描述

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

推荐答案

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

其他推荐答案

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

其他推荐答案

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.