Get child node values from Linq C# - Stack Overflow

Trying to get values of some child elements in XML, however, when retrieving the values of these elemen

Trying to get values of some child elements in XML, however, when retrieving the values of these elements, they are stitched together.

Also, sometimes the file can have 1 reference number but there can also be multiple.

Code below

public class Test
{
    public static void ParseXml(string xml)
    {
        var doc = XDocument.Parse(xml);
        List<KeyValuePair<string, string>> caseList = new List<KeyValuePair<string, string>>();
        var t = 1; 
        foreach (var el in doc.Descendants("ReferenceNumbers"))
        {
            //Console.WriteLine(el.ToString());
            caseList.Insert(0, new KeyValuePair<string, string>(t.ToString(), el.Value));
            t++;
        }

        for (int x = 0; x < caseList.Count; x++)
        {
            Console.WriteLine(caseList[x]);
        }
    }
    
    public static void Main()
    {
        
        
        
        ParseXml(@"<root>
<ReferenceNumbers>
        <Reference1>CN534786</Reference1>
        <Reference2>CN476587</Reference2>
   </ReferenceNumbers>
</root>");
    }
}

and the result is

[1, CN534786CN476587]

However, I'm expecting [1, CN534786] [2, CN476587]

Fiddle

Trying to get values of some child elements in XML, however, when retrieving the values of these elements, they are stitched together.

Also, sometimes the file can have 1 reference number but there can also be multiple.

Code below

public class Test
{
    public static void ParseXml(string xml)
    {
        var doc = XDocument.Parse(xml);
        List<KeyValuePair<string, string>> caseList = new List<KeyValuePair<string, string>>();
        var t = 1; 
        foreach (var el in doc.Descendants("ReferenceNumbers"))
        {
            //Console.WriteLine(el.ToString());
            caseList.Insert(0, new KeyValuePair<string, string>(t.ToString(), el.Value));
            t++;
        }

        for (int x = 0; x < caseList.Count; x++)
        {
            Console.WriteLine(caseList[x]);
        }
    }
    
    public static void Main()
    {
        
        
        
        ParseXml(@"<root>
<ReferenceNumbers>
        <Reference1>CN534786</Reference1>
        <Reference2>CN476587</Reference2>
   </ReferenceNumbers>
</root>");
    }
}

and the result is

[1, CN534786CN476587]

However, I'm expecting [1, CN534786] [2, CN476587]

Fiddle

Share Improve this question edited Nov 20, 2024 at 0:28 dbc 117k26 gold badges264 silver badges387 bronze badges asked Nov 19, 2024 at 21:36 jwdickinsjwdickins 235 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You might be confused on what doc.Descendants(string) actually does: it (recursively) finds the children elements of doc with the given name. So you're searching for the elements with the name "ReferenceNumbers", not getting the children of the tag with the name "ReferenceNumbers".

The for loop will only iterate over the single <ReferenceNumbers> element, not its children. Taking the .Value of this element just concatenates the values of its children, which is why you got the two reference numbers combined.

Use doc.Descendants(string).Elements() instead, to iterate over the (immediate) children of the found <ReferenceNumbers> element:

foreach (var el in doc.Descendants("ReferenceNumbers").Elements())
{
    caseList.Insert(0, new KeyValuePair<string, string>(t.ToString(), el.Value));
    t++;
}

which produces:

[2, CN476587]
[1, CN534786]

This would also handle the case of there being multiple <ReferenceNumbers> elements. If there were multiple, all of their children would combined into the same caseList list.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742397049a4436160.html

相关推荐

  • Get child node values from Linq C# - Stack Overflow

    Trying to get values of some child elements in XML, however, when retrieving the values of these elemen

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信