LINQ to XML - 如何有效地访问XML中的内部标记?

时间:2016-07-16 09:54:41

标签: c# linq linq-to-xml

我有一个如下所示的XML:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <data>
    <dataname>A</dataname>
    <property>
      <name>One</name>
      <value>1</value>
    </property>
    <property>
      <name>Two</name>
      <value>2</value>
    </property>
  </data>
  <data>
    <dataname>B</dataname>
    <property>
      <name>One</name>
      <value>11</value>
    </property>
    <property>
      <name>Two</name>
      <value>22</value>
    </property>
  </data>
</root>

我正在尝试在Visual Studio(c#)中编写一个函数,该函数从value对中返回name - value部分。到目前为止,我可以实现以下目标 (我知道,目前没有错误检查):

//private static XElement xml = new XElement("root");
//xml = XElement.Load("Defaults.xml"); -> this happens on program initialization
// The program accesses it several times, so I'm keeping it in the memory to avoid excess HDD usage

public int GetDefaultValue(string dataBlock, string propName)
{
    var xx =
        from first in xml.Elements("data")
        where (string)first.Element("dataname") == dataBlock
        select first;

    var yy =
        from second in xx.Elements("property")
        where (string)second.Element("name") == propName
        select second;

    return int.Parse(yy.First().Element("value").Value);
}

我认为这看起来非常糟糕,在阅读了大量关于LINQ to XML的MSDN之后,我认为我不会更接近于使它成为更好的代码。每当我尝试使用单个变量执行此操作时,我从未得到过我想要的内容。

有人可以帮助我让这更简单吗? (所以我可能会知道我做错了什么)

3 个答案:

答案 0 :(得分:1)

您可以在一个查询中执行此操作:

public int GetDefaultValue(string dataBlock, string propName)
{
    var val =
            from data in xml.Elements("data")
            where (string)data.Element("dataname") == dataBlock
            from property in data.Elements("property")
            where (string)property.Element("name") == propName
            select property.Element("value");

    return (int)val.First();
}

答案 1 :(得分:1)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            int results = test.GetDefaultValue("A", "One");
        }
    }
    public class Test
    {
        const string FILENAME = @"c:\temp\test.xml";
        XDocument xml = null;
        public Test()
        {
            xml = XDocument.Load(FILENAME);
        }


        public int GetDefaultValue(string dataBlock, string propName)
        {

            return xml.Descendants("data")
                .Where(x => (string)x.Element("dataname") == dataBlock)
                .Descendants("property")
                .Where(y => (string)y.Element("name") == propName)
                .Select(z => (int)z.Element("value")).FirstOrDefault();

        }
    }
}

答案 2 :(得分:0)

对于可能遇到同样问题的VB用户。

Public Function GetDefaultValue(dataBlock As String, propName As String) As Integer
    'note - returns zero if not found
    Return (From d In xe.Elements
            Where d.<dataname>.Value = dataBlock
            From p In d.<property>
            Where p.<name>.Value = propName
            Select CInt(p.<value>.Value) Take 1).FirstOrDefault
End Function

使用此数据

Dim xe As XElement = <root>
                         <data>
                             <dataname>A</dataname>
                             <property>
                                 <name>One</name>
                                 <value>1</value>
                             </property>
                             <property>
                                 <name>Two</name>
                                 <value>2</value>
                             </property>
                         </data>
                         <data>
                             <dataname>B</dataname>
                             <property>
                                 <name>One</name>
                                 <value>11</value>
                             </property>
                             <property>
                                 <name>Two</name>
                                 <value>22</value>
                             </property>
                         </data>
                     </root>