-2

Possible Duplicate:
How to read XML node from URL using C#?

I'm having below XML, I want to read this XML URL using C#

<result>
<Accounts>
<row no="1">
<FL val="ACCOUNTID">012345678</FL>
<FL val="SMOWNERID">012345678</FL>
<FL val="Account Owner">
<![CDATA[ demo name]]>
</FL>
<FL val="Account Name">
<![CDATA[ demo ]]>
</FL>
<FL val="Phone">
<![CDATA[ +12 34 5567 345]]>
</FL>
<FL val="Account Site">
<![CDATA[ demo]]>
</FL>

I have used below code for reading XML from URL but i'm getting below output in console

<FL val="Account Name">
<![CDATA[ demo ]]>
</FL>
<FL val="Phone">
</FL>
<FL val="Account Site">
</FL>

My code is below :

String xmlURL = "http://localhost/my.xml";
        XmlTextReader xmlReader = new XmlTextReader(xmlURL);
        while (xmlReader.Read())
        {
            switch (xmlReader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.
                    Console.Write("<" + xmlReader.Name);

                    while (xmlReader.MoveToNextAttribute()) // Read the attributes.
                        Console.Write(" " + xmlReader.Name + "=’" + xmlReader.Value + "’");
                    Console.WriteLine(">");
                    break;
                case XmlNodeType.Text: //Display the text in each element.
                    Console.WriteLine(xmlReader.Value);
                    break;
                case XmlNodeType.EndElement: //Display the end of the element.
                    Console.Write("</" + xmlReader.Name);
                    Console.WriteLine(">");
                    break;
            }
        }
        Console.WriteLine("Press any key to continue…");
        Console.ReadLine(); //Pause

Please help to read the inside data

2
  • 2
    Any reason you want to use a low-level XML API instead of the (much simpler) LINQ to XML? Commented Sep 4, 2012 at 12:47
  • 2
    If you're on .NET 3.5+, you can use LINQ to XML (XDocument), which I think is much easier. Commented Sep 4, 2012 at 12:47

4 Answers 4

1

XmlReader and subclasses are just about the hardest approach. Unless you need to avoid loading the whole parsed object graph into memory it is better to avoid.

Using XDocument and XPath (using System.Xml.XPath to get the extensions) is easier:

var doc = XDocument.Load(path);
var iterator = doc.XPathSelectElements("/result/Accounts/row/FL");
foreach (var flNode in iterator) {
  var text = flNode.Value;
}
Sign up to request clarification or add additional context in comments.

2 Comments

The only problem is that xml seems to contain many accounts, and many rows in one account. Flatten all the data is not a good idea.
@L.B Too much ambiguity in the Q to improve it. Getting all the text seems odd, but that's what the last sentence implies.
0

Have a look at using XPath instead of raw XML parsing like you're doing.

Refer to this link for more information about XPath.

Comments

0

try create XMLDocument, then populate it with file and in the end pass thgrough in for loop.

string output;

XmlDocument myxml = new XmlDocument(); //create XML document object

myxml.LoadXml("path to your document"); //populate doc with youe doumnet

XmlElement my_xml_element = myxml.DocumentElement; 

XmlAttributeCollection attribute = myElement.Attributes;


foreach (XmlAttributeCollection test_atribute in attribute )
{
   output = test_attribute.Name
}

Comments

0

Use LINQ2XML..Its COOL

XElement doc=XElement.Load("yourXML");
foreach(XElement x in doc.Descendants("results").Descendants("Accounts").Descendants("row").Elements())
{
x.Name;//Its name
x.Attribute("val").Value;//its val attributes value
x.Value;//its value
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.