Not getting Xml portion in formatted form?

2019-03-02 17:38发布

I am trying to create a xml file which is perfectly formatted. It include somethings Element getting replaced later on. The output is not perfectly formatted.

xEvent contains a whole xml template form like string

Here is the most of the relevant code

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;

I am expecting the xml of the following form

 <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>

But my output is coming as

 <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条回答
登录 后发表回答