blah blah blah is here! blah blah » Close

up0down
link

Hi
I have this xml file
<?xml version="1.0" encoding="utf-8"?>
<book value="RUT">
<title>Rut</title>
<text>
<p>
<div>
<chapter value="1" />
<verse value="1" /> string <verseEnd />
<verse value="2" /> string <verseEnd />
<verse value="3" /> String <verseEnd/>
<chapterEnd/>
</div>
</p>
<p>
<div>
<chapter value="2" />
<verse value="1" /> string <verseEnd />
<verse value="2" /> string2 <verseEnd />
<verse value="3" /> string3 <verseEnd />
</div>
</p>
</text>
</book>
Can same one explain me how to read data from xml.
for example how can i get value from chapter2 verse2 (string2)
thanks
dav

last answered 2 years ago

1 answers

up0down
link

There are a number of ways to read nodes from an Xml file but the one I usually prefer is to load the XmlFile into an XmlDocument object and then use XPath queries to isolate specific nodes.
If you're not familiar with XPath there's a good online tutorial <a target="_new" href="http://www.w3schools.com/XPath/default.asp">here</a>.
For example, the following program gets the value from chapter 2, verse 2:
using System;
using System.Xml;
using System.Xml.XPath;
class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("david2.xml");
XmlNode node = doc.SelectSingleNode("//chapter[@value='2']");
node = node.SelectSingleNode("following-sibling::verse[@value='2']");
node = node.NextSibling;
if (node == null) return;
Console.WriteLine(node.InnerText.Trim());
Console.ReadKey();
}
}

up0down
link

Thanks Vulpes! It works!
But now I'm not able to access the last child, and i don't know why!
I have this kind of xml:
<?xml version="1.0" encoding="utf-8"?>
<scripture>
<canon id="OT" value="OT">
<book value="GEN" >
<title >Title</title>
<text>
<p>
</p>
<p>
<div>
<chapter value="1" />
<verse value="1" />text <verseEnd />
<chapterEnd />
</div>
</p>
</text>
</book>
</canon>
</scripture>
I'm not able to access the TEXT value from chapter1, verse1 ?
How can i do that?
Thanks!
David

up0down
link

The code for that will be similar to what we had before except that the value attributes will now be '1' rather than '2':
XmlNode node = doc.SelectSingleNode("//chapter[@value='1']");
node = node.SelectSingleNode("following-sibling::verse[@value='1']");
node = node.NextSibling;
if (node == null) return;
Console.WriteLine(node.InnerText.Trim());
In XPath, the "//" looks for whatever follows it, wherever it is in the document.
Just to make sure, I tried the above code on the new file and it worked fine.
Are you sure you've including the call to the NextSibling property? That's needed because 'text' isn't a child of the verse node - it stands on its own.

up0down
link

Ok, so if i want all the verse atributes from chapter 1, it is correct if i do like this:
XmlNodeList capitoleList = doc.SelectNodes("//book[@value='GEN']//chapter[@value='1']");
XmlNode node = doc.SelectSingleNode("//book[@value='GEN']");
node = node.SelectSingleNode("following-sibling::chapter[@value='1']");
node = node.NextSibling;
rich_preview.Text += node.InnerText;
david

up0down
link

I'm trying to get all the verse atributes from gen, where chapter is 1, but no success!
What i'm doing wrong ?
XmlNodeList capitoleList = doc.SelectNodes("//book[@value='GEN']//chapter[@value='1']//verse");
MessageBox.Show(capitoleList.Count.ToString());
foreach (XmlNode node in capitoleList)
{

XmlElement capitolElement = (XmlElement)node;
string c = capitolElement.Attributes[0].InnerText.Trim();
rich_preview.Text += c;
}
david

up0down
link

Try it like this:
XmlNode n = doc.SelectSingleNode("//book[@value='GEN']");
n = n.SelectSingleNode("descendant::chapter[@value='1']");
XmlNodeList capitoleList = n.SelectNodes("following-sibling::verse");
MessageBox.Show(capitoleList.Count.ToString());
foreach (XmlNode node in capitoleList)
{
XmlElement capitolElement = (XmlElement)node;
string c = capitolElement.Attributes[0].InnerText.Trim();
rich_preview.Text += c;
}
If you're using your last document, that should just print out '1' to the RTB i.e. the attribute of the only verse node of chapter 1.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback