0

I am trying to read the following file, I can read the attributes, but I can't go into the specific element (Address in this case) and read its elements based on the attribute of that (Address) element. Shortly I need to distinguish between work and home addresses. I need to do this with XMLReader class. Can you help?

    <Address Label="Work">
       <Name>Name1</Name> 
       <Street>PO 1</Street> 
       <City>City1</City> 
       <State>State 1</State> 
    </Address>
    <Address Label="Home">
       <Name>Name2</Name> 
       <Street>PO 2</Street> 
       <City>City2</City> 
       <State>State 2</State>  
    </Address>"
1
  • Can you post what you have tried so far? (i assume you are using C#) Commented Mar 1, 2012 at 15:02

3 Answers 3

2

Okay, here are some notes to think about. XMLReader in the sense i understand you use it (with no code example) is that you iterate over the document, since the XMLReader is forward-only, and read-only.

Because of this you need to iterate until you find the node you need. In the example below i find the address element labeled "work" and extract that entire node. Then query on this node as you want.

using (var inFile = new FileStream(path, FileMode.Open))
{
    using (var reader = new XmlTextReader(inFile))
    {
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    if (reader.Name == "Address" && reader.GetAttribute(0) == "Work")
                    {
                        // Create a document, which will contain the address element as the root
                        var doc = new XmlDocument();
                        // Create a reader, which only will read the substree <Address> ... until ... </Address>
                        doc.Load(reader.ReadSubtree());
                        // Use XPath to query the nodes, here the "Name" node
                        var name = doc.SelectSingleNode("//Address/Name");
                        // Print node name and the inner text of the node
                        Console.WriteLine("Node: {0}, Inner text: {1}", name.Name, name.InnerText);
                    }
                    break;
            }
        }
    }
}

Edit

Made an example that not uses LINQ

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I got to the Address Node and select the one with work, how do I go to the name or street tag value?
Hi, i have updated the answer with an example of accessing the street element.
Thanks, unfortunately Linq is not an option, programmin in .NET CF 2.0, is there any other sugesstion you can make? Thanks in advance.
Okay, i have updated the example to create a xmlDocument with a reader that only reades the sub tree when the address node is located. This document can then be queried, and this should be without LINQ
Thank you, appricate your time.
2

XML:

<Countries>
  <Country name ="ANDORRA">
    <state>Andorra (general)</state>
    <state>Andorra</state>
  </Country>
  <Country name ="United Arab Emirates">
    <state>Abu Z¸aby</state>
    <state>Umm al Qaywayn</state>
  </Country>

Java:

 public void datass(string file)
  {

            string file = HttpContext.Current.Server.MapPath("~/App_Data/CS.xml");
                XmlDocument doc = new XmlDocument();
                if (System.IO.File.Exists(file))
                {
                    //Load the XML File
                    doc.Load(file);

                }


                //Get the root element
                XmlElement root = doc.DocumentElement;
                XmlNodeList subroot = root.SelectNodes("Country");

                for (int i = 0; i < subroot.Count; i++)     
                {

                    XmlNode elem = subroot.Item(i);
                    string attrVal = elem.Attributes["name"].Value;
                    Response.Write(attrVal);
                    XmlNodeList sub = elem.SelectNodes("state");
                    for (int j = 0; j < sub.Count; j++)
                    {
                        XmlNode elem1 = sub.Item(j);
                        Response.Write(elem1.InnerText);

                    }
                }

    }

Comments

1

Using XPath you can easily write concise expressions to navigate an XML document.

You would do something like

XmlDocument xDoc = new XmlDocument();

xDoc.LoadXml(myXMLString);

XmlNode homeAddress = xDoc.SelectSingleNode("//Address[@Label='Work']");

Then do whatever you want with homeAddress.

Read more here on w3schools on XPath.

2 Comments

Hi, Thanks for the answer but I am trying to use XMLReader for its speed.
And I must say ("//Address[@Label='Work']"); is very slick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.