在C#中附加现有的Xml文件 [英] Append existing Xml File in C#

查看:82
本文介绍了在C#中附加现有的Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试附加一个xml文件来添加新的D100。这是我到目前为止的xml和代码。



I am trying to append a xml file to add a new D100. Here is my xml and code I have so far.

<flp:Tab xmlns:flp="http://www.w3.org/2001/XMLSchema"   Title="Testing">
  <flp:Form Number="0" id="1005" />
  <flp:Rev Time="2013-01-21T15:08:00">
    <flp:Author Name="Brad" Aid="15" />
  </flp:Rev>
  <flp:Designs Id="D100">
    <flp:D100 Number="1">
      <flp:Code>A</flp:Code>
      <flp:Documented>true</flp:Documented>
      <flp:Note>In Process</flp:Note>
      <flp:Testers>
        <flp:Tester Name="David">
          <flp:Titles>
            <flp:Title Number="0" Name="Entry 1">
              <flp:Start>Begin</flp:Start>
              <flp:Finish>End</flp:Finish>
            </flp:Title>
          </flp:Titles>
        </flp:Tester>
      </flp:Testers>
      <flp:TestGivers>
        <flp:TestGiver Name="James" />
      </flp:TestGivers>
      <flp:IsRequired>true</flp:IsRequired>
      <flp:IsOptional>false</flp:IsOptional>
    </flp:D100>
  </flp:Designs>
</flp:Tab>









这是C#代码







Here is the C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;


namespace AppendXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Desktop\\Temp.xml");

            //XmlElement root = doc.CreateElement("Tab");

            XmlElement D100 = doc.CreateElement("D100");
            D100.SetAttribute("Number", "2");

            XmlElement Code = doc.CreateElement("Code");
            Code.InnerText = "B";

            XmlElement Documented = doc.CreateElement("Documented");
            Documented.InnerText = "false";

            XmlElement Note = doc.CreateElement("Note");
            Note.InnerText = "Complete";

            XmlElement Tester = doc.CreateElement("Tester");
            Tester.SetAttribute("Name", "John");

            XmlElement Title = doc.CreateElement("Title");
            Title.SetAttribute("Number", "0");
            Title.SetAttribute("Name", "Ronald");

            XmlElement Start = doc.CreateElement("Start");
            Start.InnerText = "Begin";

            XmlElement Finish = doc.CreateElement("Finish");
            Finish.InnerText = "End";

            XmlElement TestGiver = doc.CreateElement("TestGiver");
            TestGiver.SetAttribute("Name", "Jeremy");

            XmlElement IsRequired = doc.CreateElement("IsRequired");
            IsRequired.InnerText = "true";

            XmlElement IsOptional = doc.CreateElement("IsOptional");
            IsOptional.InnerText = "false";




            D100.AppendChild(IsOptional);
            D100.AppendChild(IsRequired);
            D100.AppendChild(TestGiver);
            D100.AppendChild(Finish);
            D100.AppendChild(Start);
            D100.AppendChild(Title);
            D100.AppendChild(Tester);
            D100.AppendChild(Note);
            D100.AppendChild(Documented);
            D100.AppendChild(Code);

            //root.AppendChild(D100);
            //doc.AppendChild(root);

            doc.Save("test13.xml");



        }
      }
    }





我错过了什么?谢谢!



What am I missing? Thanks!!

推荐答案

好的。在这里。



首先,您需要获取要添加子节点的节点,然后将子节点添加到此节点。



由于您要将您的孩子附加到此 Designs Id =D100节点,请使用以下代码首先获取此节点。
Ok. here you go.

First step you need to get the Node you want to add your child node, then add your child nodes to this node.

Since you want to Append your child to this Designs Id="D100" node, use the following code to get this node first.
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("flp", "http://www.w3.org/2001/XMLSchema");

XmlNode nextNode = doc.SelectSingleNode("/flp:Tab/flp:Designs", namespaces);



要创建每个元素,您应该附加名称空间元素 flp 以便匹配与其他元素一起,为每个Element添加名称空间使用以下代码。


To create each element you should append the namespace element flp in order to match with other elements, to add namespace to each Element use the following code.

XmlElement D100 = doc.CreateElement("flp","D100","http://www.w3.org/2001/XMLSchema");
D100.SetAttribute("Number", "2");

XmlElement Code = doc.CreateElement("flp", "Code", "http://www.w3.org/2001/XMLSchema");
Code.InnerText = "B";
D100.AppendChild(Code);



请注意,您需要重新订购要添加到D100元素的元素并添加其子节点,如另一个节点。



最后将新创建的D100节点添加到Designs子节点


Please note that you need to re-order the element to you add to the D100 element and add its child nodes like the other node.

finally add the newly created D100 node to the Designs child node

nextNode.AppendChild(D100);


在保存之前,您需要将元素添加到xml文档中,例如:

Before the save you need to add the element to the xml document, for example:
doc.AppendChild(D100);



或prhaps


or prhaps

doc.DocumentElement.AppendChild(D100);



干杯,

Edo


Cheers,
Edo


这篇关于在C#中附加现有的Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆