没有以格式化形式获取Xml部分?

时间:2018-02-05 08:34:24

标签: c# xml formatting xmldocument

我正在尝试创建一个格式完美的xml文件。它包括稍后元素被替换的东西。 输出格式不完美。

xEvent包含整个xml模板表单,如字符串

以下是大部分相关代码

string c2 = "</Caption>]]>";


string c = "<![CDATA[<Caption xmlns=\"http://www.omn.tv/iTX/OmnibusCxsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.om.t/iTX/OmnibusCn.xsd\">";
XmlDocument xml2 = new XmlDocument();
            xml2.LoadXml(xEvent);

            foreach (XmlNode itemNode in xml2.DocumentElement.ChildNodes)
            {
                XmlElement xmlElement = (XmlElement)itemNode;
                if (itemNode.ChildNodes.Count == 0)
                {

                }
                else
                {
                    foreach (XmlNode childNode in itemNode.ChildNodes)
                    {
                        if (childNode.Name == "Options")
                        {
                            foreach (XmlNode item in childNode.ChildNodes)
                            {
                                if (item.Name == "Data")
                                {
                                    foreach (XmlNode itemC in item.ChildNodes)
                                    {
                                        if (itemC.Name == "Caption")
                                        {
                                            XmlElement getTemplateID = xml2.CreateElement("TemplateID");
                                            getTemplateID.InnerText = "#TemplateID";
                                            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);


                                            //}
                                        }
                                    }
                                }
                            }
                        }








 StringWriter string_writer = new StringWriter();
                XmlTextWriter xml_text_writer = new XmlTextWriter(string_writer);
                xml_text_writer.Formatting = Formatting.Indented;
                xml2.WriteTo(xml_text_writer); // xml is your XmlDocument

            //string xEvent2 = string_writer.ToString();
   xEvent2 = xEvent2.Replace("<Caption>", c);  //replacing values 
            xEvent2 = xEvent2.Replace("</Caption>", c2); //replacing values 
            xEvent2 = xEvent2.Replace("#TemplateID", TemplateName + name);
            xEvent2 = xEvent2.Replace("#FieldID1", name2 + "1");
            xEvent2 = xEvent2.Replace("#TextString1", "\"" + str2 + "\"");
            xEvent2 = xEvent2.Replace("#FieldID2", name2 + "2");
            xEvent2 = xEvent2.Replace("#TextString2", "\"" + str3 + "\"");

  PrimaryString = xEvent2;

我期待以下形式的xml

 <Options>
  <Data><![CDATA[<Caption xmlns=\"http://www.omn.to/iTX/OmCxsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.om.t/iTX/OmnibusCn.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>
  </Options>

但我的输出是

 <Options>
      <Data><![CDATA[<Caption xmlns=\"http://www.omn.to/iTX/OmCxsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.om.t/iTX/OmnibusCn.xsd\">
      <TemplateID>T000114-NOW</TemplateID>
      <CaptionOptions>
        <CaptionField>
          <FieldID>NOW1</FieldID>
          <TextString>"Ep 01"</TextString>
          <FieldID>NOW2</FieldID>
          <TextString>""</TextString>
        </CaptionField>
      </CaptionOptions>
    </Caption>]]></Data> //Data is not alligned properly
  </Options>

0 个答案:

没有答案