Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting data from XML

Status
Not open for further replies.

mjd3000

Programmer
Apr 11, 2009
136
GB
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>



 
look into the System.Xml namespace your options include XPath queries, XmlReader, XmlDocument, LinqToXml, Regex, etc.

there are plenty of examples online.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
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?
 
I think attrResponseGroupRefID.InnerXml is the problem. instead you want attrResponseGroupRefID.Value.
You could also tighten up the code some
Code:
protected IEnumerable<string> GetRefIdsFrom(string xml)
{
  var doc = new XmlDocument();
  doc.Load(xml);
  var nodes = doc.SelectNodes("//ResponseGroup");
  foreach(var node in nodes)
  {
     yield return nodes.Attributes["refID"].Value;
  }
}
then you can call it like this
Code:
var ids = GetRefIdsFrom(Request.Form["xml"]);
var arrayOfIds = new List<string>(ids).ToArray();
lbl1.Text = string.Join(", ", arrayOfIds);

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top