2016-12-01 120 views
0

我試圖創建一個XML是這樣的:我無法創建與同一父節點不同的前綴子節點

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
- <inventory_report:inventoryReportMessage xmlns:inventory_report="urn:gs1:ecom:inventory_report:xsd:3" xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:ecom_common="urn:gs1:ecom:ecom_common:xsd:3" xmlns:shared_common="urn:gs1:shared:shared_common:xsd:3"> 
- <sh:StandardBusinessDocumentHeader> 
    <sh:HeaderVersion>Standard Business Header version 1.3</sh:HeaderVersion> 
- <sh:Sender> 
    <sh:Identifier Authority="GS1">0000</sh:Identifier> 
- <sh:ContactInformation> 
    <sh:Contact>some one</sh:Contact> 
    <sh:EmailAddress>[email protected]</sh:EmailAddress> 
    <sh:TelephoneNumber>00357</sh:TelephoneNumber> 
    <sh:ContactTypeIdentifier>IT Support</sh:ContactTypeIdentifier> 
    </sh:ContactInformation> 
    </sh:Sender> 

我使用下面的代碼創建的XML - >

var xelementNode = doc.CreateElement("inventory_report", "inventoryReportMessage","urn:gs1:ecom:inventory_report:xsd:3"); doc.AppendChild(xelementNode); var xelementSubNode = doc.CreateElement("sh", xelementNode, "StandardBusinessDocumentHeades",""); xelementNode.AppendChild(xelementSubNode);

我得到這個輸出上面的代碼 - >

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?> 
- <inventory_report:inventoryReportMessage xmlns:inventory_report="urn:gs1:ecom:inventory_report:xsd:3"> 
- <StandartBusinessDocumentHeader> 
    <HeaderVersion>Standard Business Header Version 1.3</HeaderVersion> 
- <Sender> 
    <Identifier>GS1</Identifier> 
- <ContactInformation> 
    <Contact>Turkey IT Support</Contact> 
    <EmailAddress>[email protected]</EmailAddress> 
    <TelephoneNumber>00 13</TelephoneNumber> 
    <ContactTypeIdentifier>IT Support</ContactTypeIdentifier> 
    </ContactInformation> 
    </Sender> 
    </StandartBusinessDocumentHeader> 
    </inventory_report:inventoryReportMessage> 

第二個前綴(「sh」)不起作用。有人能幫我嗎???

回答

0

序列化方法,您可以定義類:

public class ContactInformation 
{ 
    [XmlElement(ElementName = "Contact")] 
    public string Contact { get; set; } 
    [XmlElement(ElementName = "EmailAddress")] 
    public string EmailAddress { get; set; } 
    [XmlElement(ElementName = "TelephoneNumber")] 
    public string TelephoneNumber { get; set; } 
    [XmlElement(ElementName = "ContactTypeIdentifier")] 
    public string ContactTypeIdentifier { get; set; } 
} 

public class Identifier 
{ 
    [XmlAttribute("Authority")] 
    public string Authority { get; set; } 

    [XmlText] 
    public string Value { get; set; } 
} 

public class Sender 
{ 
    [XmlElement(ElementName = "Identifier")] 
    public Identifier Identifier { get; set; } 
    [XmlElement(ElementName = "ContactInformation")] 
    public ContactInformation ContactInformation { get; set; } 
} 

public class StandartBusinessDocumentHeader 
{ 
    [XmlElement(ElementName = "HeaderVersion")] 
    public string HeaderVersion { get; set; } 
    [XmlElement(ElementName = "Sender")] 
    public Sender Sender { get; set; } 
} 

[XmlRoot(ElementName = "inventoryReportMessage", Namespace = "urn:gs1:ecom:inventory_report:xsd:3")] 
public class InventoryReportMessage 
{ 
    [XmlElement("StandardBusinessDocumentHeader", Namespace = "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader")] 
    public StandartBusinessDocumentHeader Header { get; set; } 
} 

然後這些序列如下:

 var report = new InventoryReportMessage 
     { 
      Header = new StandartBusinessDocumentHeader { 
       HeaderVersion = "Standard Business Header version 1.3", 
       Sender = new Sender 
       { 
        Identifier = new Identifier 
        { 
         Authority = "GS1", 
         Value = "0000" 
        }, 
        ContactInformation = new ContactInformation 
        { 
         Contact = "some one", 
         EmailAddress = "[email protected]", 
         TelephoneNumber = "00357", 
         ContactTypeIdentifier = "IT Support" 
        } 
       } 
      } 
     }; 
     using (var stream = new MemoryStream()) 
     { 
      using (var writer = new StreamWriter(stream)) 
      { 
       var settings = new XmlWriterSettings { 
        Indent = true 
       }; 
       using (var xmlWriter = XmlWriter.Create(writer, settings)) 
       { 
        xmlWriter.WriteStartDocument(false); 
        var serializer = new XmlSerializer(typeof(InventoryReportMessage)); 
        var namespaces = new XmlSerializerNamespaces(); 
        namespaces.Add("inventory_report", "urn:gs1:ecom:inventory_report:xsd:3"); 
        namespaces.Add("sh", "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"); 
        namespaces.Add("ecom_common", "urn:gs1:ecom:ecom_common:xsd:3"); 
        namespaces.Add("shared_common", "urn:gs1:shared:shared_common:xsd:3"); 
        serializer.Serialize(xmlWriter, report, namespaces); 
       } 
       stream.Position = 0; 
       using (var reader = new StreamReader(stream)) 
       { 
        Console.WriteLine(reader.ReadToEnd()); 
       } 
      } 
     } 

     Console.ReadLine(); 
0

使用xml linq。我喜歡創建一個字符串標題,然後在代碼中添加動態值。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string header = 
      "<inventory_report:inventoryReportMessage xmlns:inventory_report=\"urn:gs1:ecom:inventory_report:xsd:3\" xmlns:sh=\"http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader\" xmlns:ecom_common=\"urn:gs1:ecom:ecom_common:xsd:3\" xmlns:shared_common=\"urn:gs1:shared:shared_common:xsd:3\">" + 
       "<sh:StandardBusinessDocumentHeader>" + 
       "<sh:HeaderVersion>Standard Business Header version 1.3</sh:HeaderVersion>" + 
       "<sh:Sender>" + 
       "</sh:Sender>" + 
       "</sh:StandardBusinessDocumentHeader>" + 
      "</inventory_report:inventoryReportMessage>"; 

      XDocument doc = XDocument.Parse(header); 

      XElement sender = doc.Descendants().Where(x => x.Name.LocalName == "Sender").FirstOrDefault(); 
      XNamespace shNs = sender.GetNamespaceOfPrefix("sh"); 

      sender.Add(new XElement(shNs + "Identifier", new object[] { 
       new XAttribute("Authority", "GS1"), 
       "0000" 
      })); 
      sender.Add( new XElement(shNs + "ContactInformation", new object[] { 
        new XElement(shNs + "Contact", "some one"), 
        new XElement(shNs + "EmailAddress", "[email protected]"), 
        new XElement(shNs + "TelephoneNumber", "00357"), 
        new XElement(shNs + "ContactTypeOdemtofier", "IT Support") 
      })); 

     } 
    } 
} 
相關問題