将richtextbox的数据插入到c#linq中的现有xml中

时间:2011-12-08 11:38:45

标签: c# xml linq

我有一个像这样的xml:

<?xml version="1.0" encoding="utf-8"?>
<assessment xmlns="http://xml.thinkcentral.com/pub/xml/hsp/assessment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:tia="http://xml.thinkcentral.com/pub/xml/hsp/tia" xmlns:tibase="http://xml.thinkcentral.com/pub/xml/hsp/tibase" xsi:schemaLocation="http://xml.thinkcentral.com/pub/xml/hsp/assessment http://xml.thinkcentral.com/pub/xml1_2_6/hsp_assessment.xsd" isbn="9780547660455" buid="NA12_AG_G01CH01A" title="Chapter 1 Test Form A" num_questions="24" num_sections="1" type="Basal" intervenable="true" duration="P5Y" pausable="false" scramble="false">
  <test_section id="1" name="Chapter 1 Test Form A" index="1">
    <aaa testitem_id="NA12_AG_G01CH01A_01" template="hsp_testitem_mc1.xslt" id="1" bankable="true">
      <tia:multipleChoiceTestItem total-points="1" questionType="Multiple Choice" sample="false" version_label="1.0">
        <tia:directions>
          <tia:tiDirectionLine>
            <tia:textBody></tia:textBody>
          </tia:tiDirectionLine>
          <tia:address>Richtextbox Data</tia:address>
        </tia:directions>
      </tia:multipleChoiceTestItem>
    </aaa>
    <aaa testitem_id="NA12_AG_G01CH01A_02" template="hsp_testitem_mc1.xslt" id="2" bankable="true">
      <tia:multipleChoiceTestItem total-points="1" questionType="Multiple Choice" sample="false" version_label="1.0">
        <tia:directions>
          <tia:tiDirectionLine>
            <tia:textBody></tia:textBody>
          </tia:tiDirectionLine>
          <tia:address>Richtextbox Data</tia:address>
        </tia:directions>
      </tia:multipleChoiceTestItem>
    </aaa>
  </test_section>
</assessment>

我必须根据aaa元素的id插入数据。

<aaa testitem_id="NA12_AG_G01CH01A_01" template="hsp_testitem_mc1.xslt" id="1" bankable="true">
<aaa testitem_id="NA12_AG_G01CH01A_02" template="hsp_testitem_mc1.xslt" id="2"bankable="true">

如果id =“1”,则ritchtextbox的数据将插入到tia:address节点。

我正在使用以下代码。

    private void button2_Click(object sender, EventArgs e)
    {
        XDocument doc = XDocument.Load(@"d:\file.xml");


      XNamespace ns = XNamespace.Get("http://tia.com");           

    var result=  (from ele in doc.Descendants("aaa")
       where ((string)ele.Attribute("id")) == "1"
       select ele.Element(ns+"address")).FirstOrDefault(); 




        if (result != null)
        {
            result.Value = richTextBox1.Text;

            doc.Save(@"d:\file.xml");
        }
        MessageBox.Show("done");

    }

它不起作用。我是怎么做到的?

1 个答案:

答案 0 :(得分:1)

首先,您发布的XML标记无效。我认为读取/写入XML文档的最简单方法是Linq-XML。您必须导入System.Xml.Linq命名空间才能使用XDocument类及其方法。看一下MSDN文章。

XDocument doc = XDocument.Load(@"c:\file.xml");

var result = (from ele in doc.Descendants("aaa")
                where ((string)ele.Attribute("id")) == "1"
                select ele.Element("address")).FirstOrDefault();

if (result != null)
{
    result.Value = richTextBox1.Text;
    doc.Save(@"c:\file.xml");
}

XML文档应该是:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <aaa id="1">
    <address>Hello World</address>
  </aaa>
  <aaa id="2">
    <address>
      write text of ritchtextbox here</address>
  </aaa>
</root>

编辑:

在OP中,XML标记存在一些问题,我修复了标记(添加了命名空间)。

<?xml version="1.0" encoding="utf-8"?>
<aaa testitem_id="chapter1" template="hsp_testitem_mc1.xslt" id="1" bankable="true" xmlns:tia="http://tia.com">
  <tia:multipleChoiceTestItem total-points="1" questionType="Multiple Choice" sample="false" version_label="1.0">
    <tia:directions>
      <tia:tiDirectionLine>
        <tia:textBody />
      </tia:tiDirectionLine>
      <tia:address>I have to edited here.(Richtextbox data)</tia:address>
    </tia:directions>
  </tia:multipleChoiceTestItem>
</aaa>

查找<tia:address>并替换其值的代码。

XDocument doc = XDocument.Load(file);

XNamespace ns = XNamespace.Get("http://tia.com");

var result = (from ele in doc.Descendants(ns + "address")
                select ele).SingleOrDefault();

if (result != null)
{
    result.Value = richTextBox1.Text;
    doc.Save(file);
}

编辑:OP在开幕式上做出更改后。

    XDocument doc = XDocument.Load(file);
    //Change the namespace
    XNamespace ns = XNamespace.Get("http://xml.thinkcentral.com/pub/xml/hsp/tia");
    var result = (
                 from ele in doc.Descendants(ns + "multipleChoiceTestItem")
                 where ele.Parent.Attribute("id").Value == "1"
                 select 
                    ele.Descendants(ns+"address").FirstOrDefault()
                 ).FirstOrDefault();

    if (result != null)
    {
        result.Value = "World";
        doc.Save(file);
    }