在c#中提取XML内部节点元素

时间:2021-11-20 01:53:08

I have an XML document that looks like this:

我有这样一个XML文档:

<root>
  <key>
    <id>v1</id>
    <val>v2</val>
    <iv>v3</iv>
  </key>
</root>

How do I extract the v2 values and v3 values of a key node using its v1 value in C#?

如何使用c#中的v1值提取关键节点的v2值和v3值?

3 个解决方案

#1


0  

I like using LINQ to XML for processing XML:

我喜欢使用LINQ到XML来处理XML:

var xml = XElement.Parse(@"<root>
                             <key>
                               <id>v1</id>
                               <val>v2</val>
                               <iv>v3</iv>
                             </key>
                           </root>");

var key = xml.Elements("key").First(x => x.Element("id").Value == "v1");

Console.WriteLine("val: " + key.Element("val").Value);
Console.WriteLine(" iv: " + key.Element("iv").Value);

I have ignored all error checking for brevity.

我忽略了所有简短的错误检查。

For example First() would throw an exception if the element is not found. You might want to use FirstOrDefault() and check for null if you are expecting that or handle edge cases a bit more gracefully.

例如,如果没有找到元素,First()将抛出异常。如果您希望使用FirstOrDefault()并检查null,或者更优雅地处理边缘情况。

Same goes for Element() calls. They might return null so calling .Value could result in a System.NullReferenceException. To avoid clutter I usually use extension methods to do these checks:

元素()调用也是如此。它们可能返回null,因此调用. value可能导致System.NullReferenceException。为了避免混乱,我通常使用扩展方法来做这些检查:

static class XElementUtilities
{
    public static string GetValue(this XElement xml, string name)
    {
        var element = xml.Element(name);
        return element == null ? null : element.Value;
    }

    public static bool ValueEqual(this XElement xml, string name, string value)
    {
        var element = xml.Element(name);
        return element != null && value != null && element.Value == value;
    }
}

#2


3  

Use Linq.

使用Linq。

 var myXml = XDocument.Parse("<root>
                                <key>
                                <id>v1</id>
                                <val>v2</val>
                                <iv>v3</iv>
                                </key>
                            </root>").Root.Elements("key")
                        .FirstOrDefault(x=> x.Element("id").Value == value);

    if (myXml  != null)
    { 
     var myObject = new 
        { 
          id = myXml.Element("id").Value, 
          val = myXml.Element("val").Value,
          iv = myXml.Element("iv").Value 
        });
    }  

Of course, you need to check for missing elements, etc, if required.

当然,如果需要,您需要检查缺少的元素等等。

#3


1  

Use xpath:

使用xpath:

/root/key[id='v1']/val
/root/key[id='v1']/iv

so something like

所以类似

myXmlDoc.SelectSingleNode("/root/key[id='v1']/val").Value
myXmlDoc.SelectSingleNode("/root/key[id='v1']/iv").Value

#1


0  

I like using LINQ to XML for processing XML:

我喜欢使用LINQ到XML来处理XML:

var xml = XElement.Parse(@"<root>
                             <key>
                               <id>v1</id>
                               <val>v2</val>
                               <iv>v3</iv>
                             </key>
                           </root>");

var key = xml.Elements("key").First(x => x.Element("id").Value == "v1");

Console.WriteLine("val: " + key.Element("val").Value);
Console.WriteLine(" iv: " + key.Element("iv").Value);

I have ignored all error checking for brevity.

我忽略了所有简短的错误检查。

For example First() would throw an exception if the element is not found. You might want to use FirstOrDefault() and check for null if you are expecting that or handle edge cases a bit more gracefully.

例如,如果没有找到元素,First()将抛出异常。如果您希望使用FirstOrDefault()并检查null,或者更优雅地处理边缘情况。

Same goes for Element() calls. They might return null so calling .Value could result in a System.NullReferenceException. To avoid clutter I usually use extension methods to do these checks:

元素()调用也是如此。它们可能返回null,因此调用. value可能导致System.NullReferenceException。为了避免混乱,我通常使用扩展方法来做这些检查:

static class XElementUtilities
{
    public static string GetValue(this XElement xml, string name)
    {
        var element = xml.Element(name);
        return element == null ? null : element.Value;
    }

    public static bool ValueEqual(this XElement xml, string name, string value)
    {
        var element = xml.Element(name);
        return element != null && value != null && element.Value == value;
    }
}

#2


3  

Use Linq.

使用Linq。

 var myXml = XDocument.Parse("<root>
                                <key>
                                <id>v1</id>
                                <val>v2</val>
                                <iv>v3</iv>
                                </key>
                            </root>").Root.Elements("key")
                        .FirstOrDefault(x=> x.Element("id").Value == value);

    if (myXml  != null)
    { 
     var myObject = new 
        { 
          id = myXml.Element("id").Value, 
          val = myXml.Element("val").Value,
          iv = myXml.Element("iv").Value 
        });
    }  

Of course, you need to check for missing elements, etc, if required.

当然,如果需要,您需要检查缺少的元素等等。

#3


1  

Use xpath:

使用xpath:

/root/key[id='v1']/val
/root/key[id='v1']/iv

so something like

所以类似

myXmlDoc.SelectSingleNode("/root/key[id='v1']/val").Value
myXmlDoc.SelectSingleNode("/root/key[id='v1']/iv").Value