Extracting data from XML

M

Mike P

I have the following XML document :

How do I go about getting for example the refID for a ResponseGroup tag,
and the refID and selected status for a ResponseOption tag?

<ScriptedFaqSession>
- <Data>
- <Section refID="Section38" sequence="1">
- <ResponseGroup refID="AnswerGroup39">
<ResponseOption refID="RadioButton141" selected="true" />
<ResponseOption refID="RadioButton142" selected="false" />
<ResponseOption refID="RadioButton145" selected="false" />
<ResponseOption refID="RadioButton152" selected="false" />
<ResponseOption refID="RadioButton154" selected="false" />
<ResponseOption refID="RadioButton155" selected="false" />
</ResponseGroup>
+ <ResponseGroup refID="AnswerGroup42">
- <ResponseText refID="Textbox9">
<Value />
</ResponseText>
- <ResponseText refID="Textbox10">
<Value />
</ResponseText>
- <ResponseText refID="Textbox11">
<Value />
</ResponseText>
- <ResponseText refID="Textbox13">
<Value />
</ResponseText>
- <ResponseText refID="Textbox12">
<Value />
</ResponseText>
</ResponseGroup>
</Section>
- <Section refID="Section1" sequence="2">
- <ResponseGroup refID="AnswerGroup1">
<ResponseOption refID="RadioButton100" selected="true" />
<ResponseOption refID="RadioButton2" selected="false" />
<ResponseOption refID="RadioButton3" selected="false" />
<ResponseOption refID="RadioButton102" selected="false" />
<ResponseOption refID="RadioButton104" selected="false" />
<ResponseOption refID="RadioButton105" selected="false" />
<ResponseOption refID="RadioButton95" selected="false" />
<ResponseOption refID="RadioButton1" selected="false" />
<ResponseOption refID="RadioButton101" selected="false" />
<ResponseOption refID="RadioButton106" selected="false" />
<ResponseOption refID="RadioButton103" selected="false" />
<ResponseOption refID="RadioButton107" selected="false" />
</ResponseGroup>
</Section>
- <Section refID="Section52" sequence="3">
- <ResponseGroup refID="AnswerGroup45">
<ResponseOption refID="AnswerGroup45.CheckboxInput1" selected="true"
/>
<ResponseOption refID="AnswerGroup45.CheckboxInput2" selected="false"
/>
<ResponseOption refID="AnswerGroup45.CheckboxInput3" selected="false"
/>
<ResponseOption refID="AnswerGroup45.CheckboxInput4" selected="false"
/>
</ResponseGroup>
- <ResponseGroup refID="AnswerGroup46">
<ResponseOption refID="AnswerGroup46.CheckboxInput1" selected="false"
/>
</ResponseGroup>
</Section>
<Section refID="Section24" sequence="4" />
</Data>
</ScriptedFaqSession>
 
B

Bryan

try XPathDocument, and use XPath queries. Fairly straight forward looking at
the MSDN docs.
 
G

Gregory A. Beamer

I have the following XML document :

How do I go about getting for example the refID for a ResponseGroup
tag, and the refID and selected status for a ResponseOption tag?

<ScriptedFaqSession>
- <Data>
- <Section refID="Section38" sequence="1">
- <ResponseGroup refID="AnswerGroup39">
<ResponseOption refID="RadioButton141" selected="true" />
<ResponseOption refID="RadioButton142" selected="false" />
<ResponseOption refID="RadioButton145" selected="false" />
<ResponseOption refID="RadioButton152" selected="false" />
<ResponseOption refID="RadioButton154" selected="false" />
<ResponseOption refID="RadioButton155" selected="false" />
</ResponseGroup>
+ <ResponseGroup refID="AnswerGroup42">
- <ResponseText refID="Textbox9">
<Value />
</ResponseText>
- <ResponseText refID="Textbox10">
<Value />
</ResponseText>
- <ResponseText refID="Textbox11">
<Value />
</ResponseText>
- <ResponseText refID="Textbox13">
<Value />
</ResponseText>
- <ResponseText refID="Textbox12">
<Value />
</ResponseText>
</ResponseGroup>
</Section>
- <Section refID="Section1" sequence="2">
- <ResponseGroup refID="AnswerGroup1">
<ResponseOption refID="RadioButton100" selected="true" />
<ResponseOption refID="RadioButton2" selected="false" />
<ResponseOption refID="RadioButton3" selected="false" />
<ResponseOption refID="RadioButton102" selected="false" />
<ResponseOption refID="RadioButton104" selected="false" />
<ResponseOption refID="RadioButton105" selected="false" />
<ResponseOption refID="RadioButton95" selected="false" />
<ResponseOption refID="RadioButton1" selected="false" />
<ResponseOption refID="RadioButton101" selected="false" />
<ResponseOption refID="RadioButton106" selected="false" />
<ResponseOption refID="RadioButton103" selected="false" />
<ResponseOption refID="RadioButton107" selected="false" />
</ResponseGroup>
</Section>
- <Section refID="Section52" sequence="3">
- <ResponseGroup refID="AnswerGroup45">
<ResponseOption refID="AnswerGroup45.CheckboxInput1" selected="true"
/>
<ResponseOption refID="AnswerGroup45.CheckboxInput2"
selected="false"
/>
<ResponseOption refID="AnswerGroup45.CheckboxInput3"
selected="false"
/>
<ResponseOption refID="AnswerGroup45.CheckboxInput4"
selected="false"
/>
</ResponseGroup>
- <ResponseGroup refID="AnswerGroup46">
<ResponseOption refID="AnswerGroup46.CheckboxInput1"
selected="false"
/>
</ResponseGroup>
</Section>
<Section refID="Section24" sequence="4" />
</Data>
</ScriptedFaqSession>

Partially code, partially pseudocode ( { ... } ):

XmlDocument doc = new XmlDocument();
//Cna use LoadXml() for a string
doc.Load(pathToXmlFile);

XmlNode node = doc.SelectSingleNode({xpath to node});

You can then for each through the ResponseGroup child nodes and further
for each for the ResponseOption nodes. You will then grab from the
attributes collection to get the ids.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
M

Mike P

I have tried the following code :

string strReceivedXML =
Convert.ToString(Request.Form["xml"]);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(strReceivedXML);

XmlNodeList xnList =
xmlDoc.SelectNodes("/ScriptedFaqSession/Data/Section/ResponseGroup");

foreach (XmlNode node in xnList)
{
XmlAttributeCollection attrCollResponseGroupRefID =
node.Attributes;
XmlAttribute attrResponseGroupRefID =
attrCollResponseGroupRefID["refID"];
lbl1.Text +=
Convert.ToString(attrResponseGroupRefID.InnerXml);
}

And I thought this would give me a listing of all refIDs for each
ResponseGroup, but it returns nothing. Can anybody help?
 
G

Gregory A. Beamer

I have tried the following code :

string strReceivedXML =
Convert.ToString(Request.Form["xml"]);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(strReceivedXML);

XmlNodeList xnList =
xmlDoc.SelectNodes("/ScriptedFaqSession/Data/Section/ResponseGroup");

foreach (XmlNode node in xnList)
{
XmlAttributeCollection attrCollResponseGroupRefID =
node.Attributes;
XmlAttribute attrResponseGroupRefID =
attrCollResponseGroupRefID["refID"];
lbl1.Text +=
Convert.ToString(attrResponseGroupRefID.InnerXml);
}

And I thought this would give me a listing of all refIDs for each
ResponseGroup, but it returns nothing. Can anybody help?


1. What is the code doing? Error? Nothing?
2. have you debugged the code in Visual Studio?

You really need to give more information here for someone to give you a
good answer. If you have not debugged, that would be the first step. One
possibility, for example, is the XML comes in encoded and needs to be
decoded before loading. That should throw an error, I assume. You also
need to see if the XPath is actually returning a node. If not, you have
the XPath set up incorrectly and need to fix that. Debugging will tell
you whether it has found a proper node or not. A quick examination of
the "Inner" properties will show you what the node contains. Very useful
information to fix the problem.

peace and grace,




--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top