How to get values from XDocument instance/object
I'm fairly new at programming using XML and I'm having a difficulty accessing/manipulating the values in that datatype. I used a string variable xmlDoc
and assigned the xml document (in string format) xmlDoc = drvEval.Row["EvalAnswers"].ToString();
When I'm trying to run this,
foreach (var answer in answers)
{
Response.Write("ddl_item=" + answer.Item + " answer=" + answer.Answer + "<br />");
}
It only gives me one output. The first node of result, and not all the contents of the xml document I parsed. Here is the complete code:
private void ddl_Eval_SelectedIndexChanged (Object sender, System.EventArgs e)
{
//Response.Write(ddl_Eval.SelectedValue.ToString() + " " + "value: " + ddl_Eval.SelectedIndex);
dtEval = new DataTable();
dtEval = data.GetEvaluation2();
DataView dvEval = dtEval.DefaultView;
string xmlDoc = String.Empty;
foreach (DataRowView drvEval in dvEval)
{
if (drvEval.Row["EvaluationID"].ToString() == ddl_Eval.SelectedValue.ToString())
{
xmlDoc = drvEval.Row["EvalAnswers"].ToString();
}
}
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(xmlDoc);
//Response.Write(xmlDoc);
var answers = from answer in xdoc.Descendants("ANSWERS")
select new
{
Answer = answer.Element("Answer").Value,
Item = answer.Element("Answer").Attribute("item").Value,
};
foreach (var answer in answers)
{
Response.Write("ddl_item=" + answer.Item + " answer=" + answer.Answer + "<br />");
}
}
and here is the XML I have from SQL (in string format, not XML datatype)
<ANSWERS>
<Answer item="ddl_3">A</Answer>
<Answer item="ddl_8">A</Answer>开发者_开发百科;
<Answer item="ddl_13">Y</Answer>
<Answer item="ddl_16">Y</Answer>
<Answer item="ddl_19">Y</Answer>
<Answer item="ddl_22">N</Answer>
<Answer item="ddl_26">Y</Answer>
<Answer item="ddl_30">N</Answer>
<Answer item="ddl_34">Y</Answer>
<Answer item="ddl_38">N</Answer>
<Answer item="ddl_42">StarLight</Answer>
</ANSWERS>
What I wanted to do is to get all the values of the Answer
and the values of the item
attribute. What am I doing wrong? Please help as I'm totally stuck. :(
I got the concept on this link: Using LINQ to XML to Add Data to XML File in C#
You should add Elements to a XML file rather than assigning only one row to the string each time (and overwriting what you've added before.).
If you want to use this approach use:
xmlDoc += drvEval.Row["EvalAnswers"].ToString();
or more effieciently
StringBuilder xmlDoc = new StringBuilder();
foreach (DataRowView drvEval in dvEval)
{
if (drvEval.Row["EvaluationID"].ToString() == ddl_Eval.SelectedValue.ToString())
{
xmlDoc.Append(drvEval.Row["EvalAnswers"].ToString());
}
}
But I recommend the Linq to XML approach:
Data To XML
or if you have lots of Data use XmlWriter or XmlTextWriter, which will create a file on disk and efficient.
XmlWriter
Whew! After doing a more thorough research, I was able to answer my own question. :D
Here's the complete code:
private void ddl_Eval_SelectedIndexChanged (Object sender, System.EventArgs e)
{
//Response.Write(ddl_Eval.SelectedValue.ToString() + " " + "value: " + ddl_Eval.SelectedIndex);
dtEval = new DataTable();
dtEval = data.GetEvaluation2();
DataView dvEval = dtEval.DefaultView;
string xmlDoc = String.Empty;
foreach (DataRowView drvEval in dvEval)
{
if (drvEval.Row["EvaluationID"].ToString() == ddl_Eval.SelectedValue.ToString())
{
xmlDoc = drvEval.Row["EvalAnswers"].ToString();
}
}
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(xmlDoc);
foreach (XElement child in xdoc.Root.Elements("Answer"))
{
Response.Write(child.Attribute("item").Value + " " + child.Value + "<br />");
}
}
Now I'm able to get the values of Answer and the values of the item attribute.
精彩评论