如何从XDocument实例/对象获取值

时间:2011-04-28 07:34:59

标签: asp.net xml

我在使用XML进行编程方面相当新,我在访问/操作该数据类型中的值时遇到了困难。我使用了字符串变量xmlDoc并分配了xml文档(字符串格式)xmlDoc = drvEval.Row["EvalAnswers"].ToString();

当我试图运行时,

foreach (var answer in answers) { Response.Write("ddl_item=" + answer.Item + " answer=" + answer.Answer + "<br />"); }

它只给我一个输出。结果的第一个节点,而不是我解析的xml文档的所有内容。这是完整的代码:

    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 />");
    }

}

这是我从SQL获得的XML(字符串格式,而不是XML数据类型)

<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>

我想要做的是获取Answer的所有值和item属性的值。我究竟做错了什么?请帮忙,因为我完全卡住了。 :(

我在这个链接上得到了这个概念:Using LINQ to XML to Add Data to XML File in C#

2 个答案:

答案 0 :(得分:0)

您应该将元素添加到XML文件,而不是每次只为字符串分配一行(并覆盖之前添加的内容。)。

如果您想使用此方法,请使用:

xmlDoc += drvEval.Row["EvalAnswers"].ToString();

或更有效率

StringBuilder xmlDoc = new StringBuilder();

foreach (DataRowView drvEval in dvEval)
{
    if (drvEval.Row["EvaluationID"].ToString() == ddl_Eval.SelectedValue.ToString())
        {
                 xmlDoc.Append(drvEval.Row["EvalAnswers"].ToString());
        }
}

但我推荐使用Linq to XML方法:

Data To XML

或者如果您有大量数据使用XmlWriter或XmlTextWriter,这将在磁盘上创建一个高效的文件。

XmlWriter

答案 1 :(得分:0)

呼!在做了更彻底的研究之后,我能够回答我自己的问题。 :d

这是完整的代码:

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 />");
    }

}

现在我可以获取Answer的值和item属性的值。