将现有孩子附加到新孩子身上?

时间:2018-02-02 12:21:01

标签: c# xml

我有一个xml文件: -

<Data>
<Caption>
</Caption>
</Data>

我在以下代码中添加了一个新的子CData

foreach (XmlNode item in childNode.ChildNodes)
{
    if (item.Name == "Data")
    {
        XmlCDataSection CData;
        CData = xml2.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");

        item.InsertBefore(CData, item.FirstChild);

        foreach (XmlNode itemC in item.ChildNodes)
        {
            if (itemC.Name == "Caption")
            {

                XmlElement getTemplateID = xml2.CreateElement("TempalteID");
                getTemplateID.InnerText = "10010";
                itemC.AppendChild(getTemplateID);

                //if (OptionsData[0].Key == "NoChilds")
                //{
                //    XmlElement Getnochild = itemDoc.CreateElement("CaptionOptions");
                //    Getnochild.InnerText = "";
                //    itemC.AppendChild(Getnochild);
                //}
                //else
                //{
                XmlNode elemCap = xml2.CreateNode(XmlNodeType.Element, "CaptionOptions", null);
                itemC.AppendChild(elemCap);
                XmlElement Getelem1 = xml2.CreateElement("CaptionField");
                elemCap.AppendChild(Getelem1);
                XmlElement elem2 = xml2.CreateElement("FieldID");
                XmlElement elem3 = xml2.CreateElement("FieldID");

                elem2.InnerText = "#FieldId1";
                elem3.InnerText = "#FieldId2";


                XmlElement elem4 = xml2.CreateElement("TextString");
                XmlElement elem5 = xml2.CreateElement("TextString");
                elem4.InnerText = "#TextString1";
                elem5.InnerText = "#TextString2";
                Getelem1.AppendChild(elem2);
                Getelem1.AppendChild(elem4);
                Getelem1.AppendChild(elem3);
                Getelem1.AppendChild(elem5);


                //}
            }
        }
    }
}

现在我的最终xml将以

的形式出现
<Data>
  <![CDATA[All Jane Austen novels 25% off starting 3/23!]]>
  <Caption>
    <TempalteID>10010</TempalteID>
    <CaptionOptions><CaptionField>
    <FieldID>#FieldId1</FieldID>
    <TextString>#TextString1</TextString>
    <FieldID>#FieldId2</FieldID>
    <TextString>#TextString2</TextString>
    </CaptionField></CaptionOptions>
  </Caption>
</Data>

但我想按照以下方式制作......

<Data><![CDATA[<Caption xmlns="http://www.iin.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.happy.xsd">
    <TemplateID>T000114-NOW</TemplateID>
    <CaptionOptions>
      <CaptionField>
        <FieldID>NOW1</FieldID>
        <TextString>"Ep 01"</TextString>
      </CaptionField>
      <CaptionField>
        <FieldID>NOW2</FieldID>
        <TextString>""</TextString>
      </CaptionField>
    </CaptionOptions>
  </Caption>]]>
</Data>

因此,您可以看到我基本上希望我新添加的孩子CData成为<Caption>的孩子。所以基本上我想创造一个孩子并让现有的孩子成为孩子。(但我不认为我仍会得到相同的格式?&#34;) 怎么做?

2 个答案:

答案 0 :(得分:1)

使用xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

using System.Runtime.InteropServices;

namespace ConsoleApplication23
{
    class Program
    {

        static void Main(string[] args)
        {
            string xml = "<Data><Caption  xmlns=\"http://www.iin.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.happy.xsd\"/></Data>";

            XDocument doc = XDocument.Parse(xml);
            XElement caption = doc.Descendants("Data").FirstOrDefault();

            caption.Add(new XElement("TemplateID","T000114-NOW"));

            XElement options = new XElement("CaptionOptions");
            caption.Add(options);

            foreach (Field field in Field.fields)
            {
                XElement newField = new XElement("CaptionField", new object[] { new XElement("FieldID", field.ID), new XElement("TextString", field.text)});
                options.Add(newField);
            }



        }
    }
    public class Field
    {
        public static List<Field> fields = new List<Field>() {
            new Field() {  ID = "NOW1", text = "Ep 01"},
            new Field() { ID = "Ep 01", text = ""}
        };

        public string ID { get; set; }
        public string text { get; set; }
    }

}

答案 1 :(得分:0)

如果我理解正确,您要做的是将XML数据存储在CDATA元素中。我想提醒您,这不是CDATA的用途,但这里有一个例子:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
            "<title>Pride And Prejudice</title>" +
            "</book>");


//Add the new node to the document.
XmlElement root = doc.DocumentElement;


XmlCDataSection CData;
CData = doc.CreateCDataSection(root.InnerXml);

root.RemoveAll();
root.AppendChild(CData);

doc.Save(Console.Out);