I'm having below XML structure in URL. Now the problem is reading XML data. I can able to read the FL val ="ACCCOUNTID" data ie 12345. But can't able to read "Account Name" ie .. I need to read demo1 data ..only.
no="1">
val="ACCOUNTID">12345
val="Account Name">
no="2">
val="ACCOUNTID">12345
val="Account Name">
My program is below :
String xmlZohoResponse = "http://localhost/demo.xml";
XmlTextReader xmlReader = new XmlTextReader(xmlZohoResponse);
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
VulpesPosted Sep 7, 2012, 6:33 AM
using System;
using System.Linq;
using System.Xml.Linq;
class Test
{
static void Main()
{
XElement result = XElement.Load("http://localhost/demo.xml");
var fls = from row in result.Element("Accounts").Elements("row") where row.Attribute
("no").Value == "1" from fl in row.Elements("FL") select fl;
string accountId = fls.ElementAt(0).Value;
Console.WriteLine("Account Id is {0}", accountId);
string accountName = (fls.ElementAt(1).FirstNode as XCData).Value.Trim();
Console.WriteLine("Account Name is {0}", accountName);
Console.ReadKey();
}
}
The output should be:
Account Id is 12345
Account Name is demo1