Convert the stream reader to xmldocument and then read nodes from xml document
using (StreamReader reader = new StreamReader(isoFileStream))
{
displayXmlData.Text = reader.ReadToEnd();
XDocument offlineHeadline = XDocument.Load(reader);
}
You're reading all the data out of the StreamReader via ReadToEnd, and then you're trying to load it into an XDocument. There's no more data to read!
Reading Data from xmldocument sample
Just like you do for getting something from the CNode you also need to do for the ANode
XDocument doc = XDocument.Load("input.xml");
XElement root = doc.Root;
foreach (XElement e in root.Elements("bar"))
{
Console.WriteLine("Elements : " + e.Value);
}
foreach (XElement e in root.Descendants("bar"))
{
Console.WriteLine("Descendants : " + e.Value);
}
Result:
Elements : Test 1
Elements : Test 3
Descendants : Test 1
Descendants : Test 2
Descendants : Test 3
LINQ DEMO:
XElement root = XElement.Parse(@"<Root>
<Paragraph>
<Text>This is the start of</Text>
</Paragraph>
<Comment>
<Text>This comment is not part of the paragraph text.</Text>
</Comment>
<Paragraph>
<Annotation Emphasis='true'>
<Text> a sentence.</Text>
</Annotation>
</Paragraph>
<Paragraph>
<Text> This is a second sentence.</Text>
</Paragraph>
</Root>");
// LINQ to XML query Merge two records
string str1 =
root
.Elements("Paragraph")
.Descendants("Text")
.Select(s => s.Value)
.Aggregate(
new StringBuilder(),
(s, i) => s.Append(i),
s => s.ToString()
);
// LINQ to XML query two records list
var str12 =
root.Elements("Paragraph").Descendants("Text").ToList();
If you need more assistance, please let me know.
If you think one reply solves your problem, please mark it as An Answer.
Member
87 Points
194 Posts
How to read XML text
Sep 26, 2013 03:19 AM|AL MUBARAK|LINK
Dear Guys,
I need an help from reading XML document in C#, here below is my code.
string str = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><subscribe-response><response><msisdn>971554728787</msisdn><opid>268</opid><responsestatus><code>1</code><description>success</description></responsestatus><service id='49142'><product clubid='22772' id='2545'><subid>1794002632</subid><substatus>ACTIVE</substatus></product></service></response></subscribe-response>";
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(new System.IO.StringReader(str));
reader.Read();
DataSet ds = new DataSet();
ds.ReadXml(reader);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0]["response_id"].ToString() == "1")
{
string test = "test";
}
}
reader.Close();
ds.Dispose();
NOTE : why didn't get response code as mentioed in "1" in the XML Document. and i got only msisdn, opid, response_id as "0". !!!
Please help me on this...
Waiting for your kind reply...
Al Mubarak
Member
31 Points
26 Posts
Re: How to read XML text
Sep 26, 2013 03:27 AM|deeptendra|LINK
Hello,
Is there a reason you are creating a string variable and hardcoding your xml data to the string?
I personally feel XML - Deserialization is a better way to read xml data from any location.
We can provide more details if you need.
Thanks,
Deeptendra
Member
87 Points
194 Posts
Re: How to read XML text
Sep 26, 2013 03:34 AM|AL MUBARAK|LINK
Dear Deeptendra,
No, the XML datas' are returned with runtime..!! and for testing i just make as string.!!
Can you please explain any other way to achieve it.!!
Al Mubarak
Contributor
4056 Points
1101 Posts
Re: How to read XML text
Sep 26, 2013 03:44 AM|prk_in|LINK
Convert the stream reader to xmldocument and then read nodes from xml document
using (StreamReader reader = new StreamReader(isoFileStream))
{
displayXmlData.Text = reader.ReadToEnd();
XDocument offlineHeadline = XDocument.Load(reader);
}
You're reading all the data out of the StreamReader via ReadToEnd, and then you're trying to load it into an XDocument. There's no more data to read!
Reading Data from xmldocument sample
Just like you do for getting something from the CNode you also need to do for the ANode
XmlNodeList xnList = xml.SelectNodes("/Element[@*]");
foreach (XmlNode xn in xnList)
{
XmlNode anode = xn.SelectSingleNode("ANode");
if (anode!= null)
{
string id = anode["ID"].InnerText;
string date = anode["Date"].InnerText;
XmlNodeList CNodes = xn.SelectNodes("ANode/BNode/CNode")
foreach (XMlNode node in CNodes)
{
XmlNode example = node.SelectSingleNode("Example");
if (example != null)
{
string na = example["Name"].InnerText;
string no = example["NO"].InnerText;
}
}
}
}
http://www.csharp-examples.net/xml-nodes-by-name/
You can also use xpath
http://msdn.microsoft.com/en-us/library/d271ytdx.aspx
Happy Coding !
Please Mark If it helps you || Ramakrishna.p
Member
87 Points
194 Posts
Re: How to read XML text
Sep 26, 2013 04:35 AM|AL MUBARAK|LINK
Hi Prk_in,
Can you please tell me the above XML response to taken the <code>1</code> values as you mentioned.
Waiting for your reply.
Al Mubarak
Participant
960 Points
288 Posts
Re: How to read XML text
Sep 27, 2013 05:25 AM|galeny|LINK
hi AL MUBARAK ,
From what I understand you want to to read the node values in xml file.
I would suggest you to try the following workground.
Read XDocument:
Code:
Result:
Elements : Test 1
Elements : Test 3
Descendants : Test 1
Descendants : Test 2
Descendants : Test 3
LINQ DEMO:
If you need more assistance, please let me know.