我写这样的代码
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>
有没有办法改变它?
答案 0 :(得分:0)
正如我所看到的,仅在第一个节点中的命名空间有所区别......(或者这是我的错误?)
替换此行:
xmlMultiAirInfo.WriteStartElement("ns", "cancelreservation", "http://www.opentravel.org/ota/2003/05");
有了这个。
xmlMultiAirInfo.WriteStartElement("ns", "cancelreservation", "");
好吧,C#做得很好。因为你的元素不知道这样的前缀。 在您的示例中,“cancelreservation”不是根元素,但如果它是root,则必须存在xmlns属性。