开发者

How does one read an XML file which has children with the same name as their ancestors?

I have trouble reading the below XML file on C# Visual web dev 2010. As you can see Category can be a name of child as well as a parent. When I try xmldataset.ReadXml it will come up with an error which says table Category can not be a child of itself.

Is there any other way to read this kind of XML.

A quick sample guide will be much appreciated.

<Categories>
  <Category id="10000000">
    <name>Clothing</name>
    <Children>
      <Category id="10010000">
        <name>Handbags & Luggage</name>
        <Children>
          <Category id="10010800">
            <name>Travel Accessories</name>
          </Category>
        </Childr开发者_开发技巧en>
      </Category>
    </Children>
  </Category>
</Categories>


I think xpath will give you what you want.

XmlNodeList nodes = myXmlDoc.SelectNodes("//name");

The "//" should return all <name> nodes where ever they are in the xml document. You can then do what ever you want with the list of <name> nodes. For example

foreach (XmlNode node in nodes)
{
    console.WriteLine(node.InnerText);
}


For those who want to know,

I sorted it out with this:

 XmlTextReader xr = new XmlTextReader("test.xml");

            int i=0;
            string[] ss; 
            while (xr.Read())
            {
                Console.Write(xr.Name);
                if (xr.Name.ToString() == "name") { ss[i] = xr.ReadString(); i++ };
            }


If you just want the name fields in an array, then try the following code. There might be minor syntax errors!

XmlDocument xd;
XmlNodeList xnl;
int i;
string[] s;

xd = new XmlDocument();
xd.Load(xmlFileName);
xnl = xd.SelectNodes("//Category/name");
i = 0;
s = new string[xnl.Count];
foreach(XmlNode xn in xnl)
{
    // xn will be an object of type XmlElement,
    // which exposes the value of the text it contains through the InnerText property
    s[i++] = xn.InnerText;
}

// an array of strings named 's' is available at this point.
// NB: I have omitted all manner of error checking here for clarity of code.

Update: I had not read your sample XML properly. I had been specifying the wrong XML path. I was specifying the path to read <Category name="Blah">...</Category> whereas your sample has <Category><name>Blah></name>...</Category>. Apologies.

Please see my comment regarding your own solution as well, though to be fair, using XmlReader is going to be a lot faster than using XmlDocument.


Using LINQ To XML the following code

XDocument xmlDoc = XDocument.Load(new StringReader(@"<Categories>
  <Category id='10000000'>
    <name>Clothing</name>
    <Children>
      <Category id='10010000'>
        <name>Handbags &amp; Luggage</name>
        <Children>
          <Category id='10010800'>
            <name>Travel Accessories</name>
          </Category>
        </Children>
      </Category>
    </Children>
  </Category>
</Categories>
"));

var names = xmlDoc.Root.Descendants("name")
        .Select(n => n.Value).ToList();

names.ForEach(n => Console.WriteLine(n));

outputs

Clothing
Handbags & Luggage
Travel Accessories

This was tested with LINQPad.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜