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

本文关键字:xml 数据 richtextbox linq 插入 | 更新日期: 2023-09-27 18:17:04

我有一个这样的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");
    }

它不工作。怎么做呢?

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

首先,您发布的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);
    }