I am using XmlReader to read through an xml file. I do not have control over the xml file and sometimes the xml file skips elements as shown below.
<Part xsi:type="Part1">
<Quantity>1</Quantity>
<Drawing>10</Drawing>
</Part>
<Part xsi:type="Part2">
<Quantity>1</Quantity>
</Part>
The Drawing and Weight are in the first entry (Part 1) but not in the second one (Part 2).
This xml has 1300 entries (Part’s) and not all entries (Part’s) have all elements as above.
My XmlReader code is below.
private void Select(string elem, ArrayList al)
{
using (XmlReader reader = new XmlTextReader(xml))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == elem && reader.IsStartElement())
{
string output = reader.ReadString();
Console.WriteLine(output);
}}}}}
How would I handle adding an element to the Arraylist when the element is not in the entry (Part) in the xml?
Update:
I modified the code to just be an output and here is the expected output:
1
10
1
” ”
I would like the output of Part 2 to display strings with a space " " for missing elements like Drawing.
I think you can try a syntax vwery close to this example:
Reading XML through LINQ
you can test existence of a node with something like this: