需要帮助在c#中使用xmltextwwriter编写XML开始元素

本文关键字:编写 xmltextwwriter XML 开始 元素 帮助 | 更新日期: 2023-09-27 17:53:26

我写的代码是这样的

  XmlDocument xdocMulticom = new XmlDocument();
  StringWriter strwMultiXml = new StringWriter();
  XmlTextWriter xmlMultiAirInfo = new XmlTextWriter(strwMultiXml);
  xmlMultiAirInfo.Formatting = Formatting.None;
  xmlMultiAirInfo.WriteStartDocument();
  xmlMultiAirInfo.WriteStartElement("ns", "cancelreservation", "http://www.opentravel.org/ota/2003/05");
  xmlMultiAirInfo.WriteStartElement("OTA_VehCancelRQ");
  xmlMultiAirInfo.WriteAttributeString("xmlns", "http://www.opentravel.org/ota/2003/05");
  xmlMultiAirInfo.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
  xmlMultiAirInfo.WriteAttributeString("Version", "1.002");
  xmlMultiAirInfo.WriteAttributeString("PrimaryLangID", "EN");
  xmlMultiAirInfo.WriteAttributeString("ReqRespVersion", "2.001");
  xmlMultiAirInfo.WriteStartElement("POS");
  xmlMultiAirInfo.WriteStartElement("Source");
  xmlMultiAirInfo.WriteStartElement("RequestorID");
  xmlMultiAirInfo.WriteAttributeString("Type", "4");
  xmlMultiAirInfo.WriteAttributeString("ID", "F0F4CCE4A9C24355");
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteStartElement("VehCancelRQCore");
  xmlMultiAirInfo.WriteAttributeString("CancelType", "Cancel");
  xmlMultiAirInfo.WriteStartElement("UniqueID");
  xmlMultiAirInfo.WriteAttributeString("Type", "14");
  xmlMultiAirInfo.WriteAttributeString("ID", "N2169641");
  xmlMultiAirInfo.WriteStartElement("PersonName");
  xmlMultiAirInfo.WriteElementString("GivenName", "Sandra");
  xmlMultiAirInfo.WriteElementString("Surname", "Jhonsan");
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteEndElement();                         
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteStartElement("VehCancelRQInfo");
  xmlMultiAirInfo.WriteStartElement("Vendor");
  xmlMultiAirInfo.WriteAttributeString("Code", "ZR");
  xmlMultiAirInfo.WriteEndElement();                          
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteEndElement();
  xmlMultiAirInfo.WriteEndElement();
  xdocMulticom.LoadXml(strwMultiXml.ToString());

生成如下输出:

<?xml version="1.0" encoding="utf-16"?>
<ns:cancelreservation xmlns:ns="http://www.opentravel.org/ota/2003/05">
  <OTA_VehCancelRQ xmlns="http://www.opentravel.org/ota/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.002" PrimaryLangID="EN" ReqRespVersion="2.001">
    <POS>
      <Source>
        <RequestorID Type="4" ID="F0F4CCE4A9C24355" />
      </Source>
    </POS>
    <VehCancelRQCore CancelType="Cancel">
      <UniqueID Type="14" ID="N2169641">
        <PersonName>
          <GivenName>Sandra</GivenName>
          <Surname>Jhonsan</Surname>
        </PersonName>
      </UniqueID>
    </VehCancelRQCore>
    <VehCancelRQInfo>
      <Vendor Code="ZR" />
    </VehCancelRQInfo>
  </OTA_VehCancelRQ>
</ns:cancelreservation>

这对我来说是可以的,但我需要它是这样的:

<?xml version="1.0" encoding="utf-16"?>
<ns:cancelreservation>
  <OTA_VehCancelRQ xmlns="http://www.opentravel.org/ota/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.002" PrimaryLangID="EN" ReqRespVersion="2.001">
    <POS>
      <Source>
        <RequestorID Type="4" ID="F0F4CCE4A9C24355" />
      </Source>
    </POS>
    <VehCancelRQCore CancelType="Cancel">
      <UniqueID Type="14" ID="N2169641">
        <PersonName>
          <GivenName>Sandra</GivenName>
          <Surname>Jhonsan</Surname>
        </PersonName>
      </UniqueID>
    </VehCancelRQCore>
    <VehCancelRQInfo>
      <Vendor Code="ZR" />
    </VehCancelRQInfo>
  </OTA_VehCancelRQ>
</ns:cancelreservation>

有办法改变吗?

需要帮助在c#中使用xmltextwwriter编写XML开始元素

正如我所见,只有第一个节点的命名空间不同…(还是我错了?)

替换这一行:

xmlMultiAirInfo.WriteStartElement("ns", "cancelreservation", "http://www.opentravel.org/ota/2003/05");

xmlMultiAirInfo.WriteStartElement("ns", "cancelreservation", "");

c#做得还不错。因为您的元素不知道这样的前缀。在您的示例中,"cancelreservation"不是根元素,但如果它要成为根元素,则必须存在xmlns属性。