I need to do validation on an XML file, these cannot be done within the schema easily, it is things like if element A has a certain value, check element B sub elements do not exist or have no value, or checking that an element's sub elements all have unique
values (ie no multiple instances of the same value).
I was wondering what is the most efficient way to traverse and read element values using C#?
thats very useful, i havent worked with XML in a good few years, but this seems a good way to traverse XML. I have drilled down to get specific values of elements. ie looping through each PayeePayPerWeek
var type = payeepayperweek.Descendants().Where(n => n.Name.LocalName == "PayeeType").FirstOrDefault().Value;
I have a question about something i cannot seem to get to work, based on the sample XML here, i would like to count the number of elements where PayeeType was equal to "example type1" and "example type2" so in this case return 2. There can be multiple PayeePayPerWeek
elements.
Also i need to check there are no multiple elements of PayeeType with the same value. So my thinking was to say count the number of element types of PayeePayPerWeek (3 in this case) and compare to a count of PayeePayPerWeek on distinct values for PayeeType?
Would this be correct and would you know the syntax to get these counts?
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
72 Posts
XML traversing C#
Jan 20, 2021 08:05 AM|mark1961|LINK
I need to do validation on an XML file, these cannot be done within the schema easily, it is things like if element A has a certain value, check element B sub elements do not exist or have no value, or checking that an element's sub elements all have unique values (ie no multiple instances of the same value).
I was wondering what is the most efficient way to traverse and read element values using C#?
All-Star
194455 Points
28077 Posts
Moderator
Re: XML traversing C#
Jan 20, 2021 08:54 AM|Mikesdotnetting|LINK
Use LINQ to XML: https://docs.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview
None
0 Points
72 Posts
Re: XML traversing C#
Jan 20, 2021 01:31 PM|mark1961|LINK
Thanks Mike,
thats very useful, i havent worked with XML in a good few years, but this seems a good way to traverse XML. I have drilled down to get specific values of elements. ie looping through each PayeePayPerWeek
I have a question about something i cannot seem to get to work, based on the sample XML here, i would like to count the number of elements where PayeeType was equal to "example type1" and "example type2" so in this case return 2. There can be multiple PayeePayPerWeek elements.
Also i need to check there are no multiple elements of PayeeType with the same value. So my thinking was to say count the number of element types of PayeePayPerWeek (3 in this case) and compare to a count of PayeePayPerWeek on distinct values for PayeeType? Would this be correct and would you know the syntax to get these counts?
None
0 Points
72 Posts
Re: XML traversing C#
Jan 20, 2021 04:54 PM|mark1961|LINK
Hi Mike
Im also trying to retrieve a PayeePayPerWeek element based on the content of a sub element ie PayeeType but all i get back is the PayeeType element.
i also tried descendentsandself but that still only bought back the PayeeType NOT PayeePayPerWeek
Contributor
2090 Points
668 Posts
Re: XML traversing C#
Jan 27, 2021 09:02 AM|XuDong Peng|LINK
Hi mark1961,
According to the xml structure you provided, the PayeePayPerWeek node is the parent node of the PayeeType node.
If you need to get the detailed information of the parent node based on the value of the child node, have you tried something like:
var nodes = emp.Descendants() .Where(n => n.Name.LocalName == "PayeeType") .Where(x => x.Value == "example type1" || x.Value == "example type2") .Select(x => x.Parent) .ToList();
Hope this can help you.
Best regards,
Xudong Peng