XML - parsing problem, elements

Markus Freitag 3,791 Reputation points
2023-02-13T16:46:01.44+00:00

Hello,

I can only find the root element, not the second element.

Why? What is wrong?

<ROOT>
       <LANEA track="1" barcode="A_1235456"/>
</ROOT>

<ROOT>
       <LANEB track="2" barcode="B_1235456"/>
</ROOT>

<ROOT>
       <LANEC track="3" barcode="C_1235456"/>
</ROOT>

This works, but without ROOT

foreach (var name in result.Root.DescendantNodes().OfType<XElement>().Select(x => x.Name).Distinct())
{
	Trace.WriteLine(name);
}

I want only a list with all elements and the root element.

XDocument result = XMLRequestsResponses.XmlResponses.ID_SERO;
string txt = result.ToString();

IEnumerable<XElement> childElements = from el in result.Elements() select el;
foreach (XElement el in childElements)
{
	Trace.WriteLine("Name: " + el.Name);

	if (el.Name.LocalName == "LANEA")
	{
		Trace.WriteLine("aa Name: " + el.Name.LocalName);
		break;
	}
	
	else if (el.Name.LocalName == "LANEB")
	{
		Trace.WriteLine("bb Name: " + el.Name.LocalName);
		break;
	}
	
	else if (el.Name.LocalName == "LANEC")
	{
		Trace.WriteLine("cc Name: " + el.Name.LocalName);
		break;
	}
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-02-14T01:55:02.4633333+00:00

    Hi, Markus Freitag
    Distinct Method returns distinct elements from a sequence.
    An XML file must have one and only one root node, and all other nodes must be its child nodes. In your example there are three root nodes.
    You can print the root node before the loop.

    Trace.WriteLine(result.Root!.Name);           
     foreach (var name in result.Root!.DescendantNodes().OfType<XElement>().Select(x => x.Name))
                {
                    switch (name.LocalName)
                    {
                        case "LANEA":
                            Trace.WriteLine("aa Name: " + name.LocalName);
                            break;
                        case "LANEB":
                            Trace.WriteLine("bb Name: " + name.LocalName);
                            break;
                        case "LANEC":
                            Trace.WriteLine("cc Name: " + name.LocalName);
                            break;
                        default:
                            Trace.WriteLine(name);
                            break;
                    }
    
                }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.