accessing XML data

M

Mike P

I have some XML (see snippet below) and I have got so far with it, but I
don't know how to get at the text within the <Value> tags :

<ResponseGroup refID="AnswerGroup184">
<ResponseText refID="Textbox25">
<Value>Mike</Value>
</ResponseText>
<ResponseText refID="Textbox26">
<Value>P</Value>
</ResponseText>
<ResponseText refID="Textbox27">
<Value>Blue Orc</Value>
</ResponseText>
<ResponseText refID="Textbox28">
<Value>01234</Value>
</ResponseText>
<ResponseText refID="Textbox29">
<Value>[email protected]</Value>
</ResponseText>
<ResponseText refID="Textbox30">
<Value>1234</Value>
</ResponseText>
<ResponseText refID="Textbox31">
<Value />
</ResponseText>
</ResponseGroup>

Here is my code :

foreach (XmlElement rGroup in
doc.SelectNodes

("/df:ScriptedFaqSession/df:Data/df:Section/df:ResponseGroup", mgr))
{
//contact details
if (rGroup.GetAttribute("refID") ==
"AnswerGroup184")
{
foreach (XmlElement rOption in
rGroup.SelectNodes
("df:ResponseText", mgr))
{
if (rOption.GetAttribute("refID").ToString()
== "Textbox25")
{

}
}
}
}

What is the correct syntax to get the text within the <Value> tags?
 
M

Martin Honnen

Mike said:
I have some XML (see snippet below) and I have got so far with it, but I
don't know how to get at the text within the <Value> tags :

<ResponseGroup refID="AnswerGroup184">
<ResponseText refID="Textbox25">
<Value>Mike</Value>
</ResponseText>
<ResponseText refID="Textbox26">
<Value>P</Value>
</ResponseText>
<ResponseText refID="Textbox27">
<Value>Blue Orc</Value>
</ResponseText>
<ResponseText refID="Textbox28">
<Value>01234</Value>
</ResponseText>
<ResponseText refID="Textbox29">
<Value>[email protected]</Value>
</ResponseText>
<ResponseText refID="Textbox30">
<Value>1234</Value>
</ResponseText>
<ResponseText refID="Textbox31">
<Value />
</ResponseText>
</ResponseGroup>

Here is my code :

foreach (XmlElement rGroup in
doc.SelectNodes

("/df:ScriptedFaqSession/df:Data/df:Section/df:ResponseGroup", mgr))
{
//contact details
if (rGroup.GetAttribute("refID") ==
"AnswerGroup184")

Note that you could simply include that condition in your XPath above,
there is no need to perform such checks after applying XPath.
So you can simply do

foreach (XmlElement rGroup in
doc.SelectNodes

("/df:ScriptedFaqSession/df:Data/df:Section/df:ResponseGroup[@refID =
'AnswerGroup184']", mgr))
{
foreach (XmlElement rOption in
rGroup.SelectNodes
("df:ResponseText", mgr))
{
if (rOption.GetAttribute("refID").ToString()
== "Textbox25")

Again that condition can be expressed with XPath further above e.g.

foreach (XmlElement rOption in
rGroup.SelectNodes
("df:ResponseText[@refID =
'Textbox25']", mgr))

What is the correct syntax to get the text within the <Value> tags?

string value =
rOption.SelectSingleNode("df:Value").InnerText;
 
S

Sreenivas

What is the correct syntax to get the text within the <Value> tags?

List<string> collection = new List<string>();

string docName = Server.MapPath(".") + "\\XMLFILE.xml";
XmlDocument doc = new XmlDocument();
doc.Load(docName);
foreach (XmlNode node in doc.SelectNodes("ResponseGroup/
ResponseText/Value"))
{
collection.Add(node.InnerText);
}
 
S

Sreenivas

List<string> collection = new List<string>();

        string docName = Server.MapPath(".") + "\\XMLFILE.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(docName);
        foreach (XmlNode node in doc.SelectNodes("ResponseGroup/ResponseText/Value"))
        {
            collection.Add(node.InnerText);
        }
Sorry i have posted it halfway, I showed you how to access the Value
attribute's value.
 
Top