Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read xml from file

Tags:

c#

xml

I got a little problem with reading information from an xml file...

The file passed to me has thousands of lines. Im interested in only 300 - 400 of those lines. I don't need to write any data back to xml when the user is finished with his operation and the data to read can be stored in a List<string>.

I found solutions on the net using an XmlTextReader to read the data. So I wouldnt have to create a class and use a Serializer. But it seems like im using the XmlTextReader wrong. Maybe you can help me...

This is how the xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<ProjectConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ProjectLists xmlns="...">
    <ProjectList>
  ... // not interested in this data
   </ProjectList>
  <ProjectList>
  <ListNode>
    <Name>Test_Environment</Name>
    <Children>
      <ListNode>
        <Name>yyy</Name>
        <Children>
          <ListNode>
            <Name>205 (ST)</Name>
            <Children>
              <ListNode>
                <Name>098-0031</Name>
                <Children />
              </ListNode>
              <ListNode>
                <Name>098-0032</Name>
                <Children />
              </ListNode>
              //more ListNodes...
            </Children>
          </ListNode>
          <ListNode>
            <Name>old</Name>
            <Children>
              <ListNode>
                <Name>W098-32</Name>
                <Children />
              </ListNode>
            </Children>
          </ListNode>
        </Children>
      </ListNode>
      <ListNode>
        <Name>xxx</Name>
        <Children />
      </ListNode>
      <ListNode>
        <Name>098-0001</Name>
        <Children />
      </ListNode>
      <ListNode>
        <Name>098-0011</Name>
        <Children />
      </ListNode>
      // More List Nodes
    </Children>
  </ListNode>
  <ListNode>
    // more List Nodes
  </ListNode>
</ProjectList>
<ProjectList>
  //more uninteresting ProjectLists...
</ProjectList>

I'm only interested in the Value of the most inner Name Elements (The first two would be "098-0031" and "098-0032").

And this is my code:

while (reader.Read()) {
            switch (reader.NodeType) {
                case XmlNodeType.Element:
                    {
                        if (reader.Name == "Name") {
                            reader.Read();
                            if (reader.Value == "Test_Environment") {
                                reader.ReadToDescendant("Children");
                                if (reader.Name == "Children") {
                                    reader.ReadToDescendant("Children");

                                }
                            }
                        }
                    }
                    break;
            }
        }

But the condition reader.Name == "Children" is never fullfilled... Can someone explain to me why. And maybe show me an easy way to store those values in a List<string>? Thanks in advance!

EDIT: I edited the xml. Sorry for that but its really hard to filter the unnecassary confusing parts from my xml...

like image 997
user3596113 Avatar asked Oct 19 '22 07:10

user3596113


1 Answers

static void GetMostInnerName()
{
    string xml = @"<ProjectConfiguration xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<ProjectLists>   
<ProjectList>
<ListNode>
<Name>Test_Environment</Name>
<Children>
<ListNode>
<Name>yyy</Name>
<Children>
<ListNode>
<Name>205 (ST)</Name>
<Children>
<ListNode>
<Name>098-0031</Name>
<Children />
</ListNode>
<ListNode>
<Name>098-0032</Name>
<Children />
</ListNode>
</Children>
</ListNode>
<ListNode>
<Name>old</Name>
<Children>
    <ListNode>
    <Name>W098-32</Name>
    <Children />
    </ListNode>
</Children>
</ListNode>
</Children>
</ListNode>
<ListNode>
<Name>xxx</Name>
<Children>
<ListNode>
<Name>098-0001</Name>
<Children />
</ListNode>
<ListNode>
<Name>098-0011</Name>
<Children />
</ListNode>
</Children>
</ListNode>
// more List Nodes
</Children>
</ListNode>
</ProjectList></ProjectLists>
</ProjectConfiguration>";
    XElement root = XElement.Parse(xml).Element("ProjectLists");
    //var xmlns = root.GetDefaultNamespace();
    //Console.WriteLine(xmlns);
    var eles = root.Elements("ProjectList").SelectMany(x => x.Elements("ListNode"));
    List<string> list = new List<string>();
    foreach (var ele in eles)
    {
        Loop(ele, list);
    }
    list.ForEach(x =>
    {
        Console.WriteLine(x);
    });
}
static void Loop(XElement ele, List<string> list)
{
    var child = ele.Element("Children");
    if (child != null && child.HasElements)
    {
        foreach (var e in child.Elements("ListNode"))
        {
            Loop(e, list);
        }
    }
    else
    {
        list.Add(ele.Element("Name").Value);
    }
}

Because your xml has many node like ProjectList,so I used SelectMany here,and I add root element to have a test,the last output is

098-0031
098-0032
W098-32
098-0001
098-0011
like image 84
Sky Fang Avatar answered Nov 15 '22 04:11

Sky Fang