使用XmlDocument格式化XML

时间:2014-10-17 08:10:36

标签: c# xml linq-to-xml

我正在尝试使用XmlDocument创建一个XML文件,然后将其传递给API。大多数数据输出正确但是有一个元素没有,无论我尝试什么。

这是我当前的输出

<agentInventoryReference xmlns="demo.org.uk/demo/AgentInventoryReference"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<agentInventoryIdentity xmlns="">
<shed xmlns="demo.org.uk/demom/AgentInventoryIdentifier">TAX</shed> 
<arrivalPort xmlns="demo.org.uk/demo/AgentInventoryIdentifier">LHR</arrivalPort> 
<masterAirwayBillPrefix xmlns="demo.org.uk/demo/AgentInventoryIdentifier">125</masterAirwayBillPrefix> 
<masterAirwayBillNumber xmlns="demo.org.uk/demo/AgentInventoryIdentifier">12345678</masterAirwayBillNumber> 
<nominatedAgent xmlns="demo.org.uk/demo/AgentInventoryIdentifier">DRB</nominatedAgent> 
</agentInventoryIdentity>

我需要放弃xmlns =&#34;&#34;在开头agentInventoryIdentity元素的末尾。

我的代码如下所示:

 XmlDocument generateInventoryXml = new XmlDocument();
 XmlElement root = generateInventoryXml.CreateElement  ("agentInventoryReference", "demo.org.uk/demo/AgentInventoryReference");
        root.SetAttribute("xmlns", "demo.org.uk/demo/AgentInventoryReference");
        root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
        generateInventoryXml.AppendChild(root);

        XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity");

        XmlElement shed = generateInventoryXml.CreateElement("shed", "demo.org.uk/Sequoia/AgentInventoryIdentifier");
        XmlElement arrivalport = generateInventoryXml.CreateElement("arrivalPort", "demo.org.uk/demo/AgentInventoryIdentifier");
        XmlElement masterAirwayBillPrefix = generateInventoryXml.CreateElement("masterAirwayBillPrefix","demo.org.uk/demo/AgentInventoryIdentifier");
        XmlElement masterAirwayBillNumber = generateInventoryXml.CreateElement("masterAirwayBillNumber", "demo.org.uk/demo/AgentInventoryIdentifier");
        XmlElement nominatedAgent = generateInventoryXml.CreateElement("nominatedAgent", "demo.org.uk/demo/AgentInventoryIdentifier");

        shed.InnerText = shedCode;
        arrivalport.InnerText = arrivalPort;
        masterAirwayBillPrefix.InnerText = awbPrefix;
        masterAirwayBillNumber.InnerText = awbNumber;
        nominatedAgent.InnerText = agent;

        agentInventoryIdentity.AppendChild(shed);
        agentInventoryIdentity.AppendChild(arrivalport);
        agentInventoryIdentity.AppendChild(masterAirwayBillPrefix);
        agentInventoryIdentity.AppendChild(masterAirwayBillNumber);
        agentInventoryIdentity.AppendChild(nominatedAgent);

        generateInventoryXml.DocumentElement.AppendChild(agentInventoryIdentity);

        GenerateInventoryXml = generateInventoryXml.InnerXml;

1 个答案:

答案 0 :(得分:0)

试试这个:

XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity",generateInventoryXml.DocumentElement.NamespaceURI);`

或者

XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity","demo.org.uk/demo/AgentInventoryReference");