我无法在同一父节点中创建具有不同前缀的子节点

时间:2016-12-01 07:10:44

标签: c# xml visual-studio

我正在尝试创建这样的XML:

parameter="goToHome"

我正在使用以下代码创建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>someone@example.com</sh:EmailAddress> <sh:TelephoneNumber>00357</sh:TelephoneNumber> <sh:ContactTypeIdentifier>IT Support</sh:ContactTypeIdentifier> </sh:ContactInformation> </sh:Sender>

我正在为上面的代码获取此输出 - &gt;

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

第二个前缀(“sh”)不起作用。有人可以帮助我吗?

2 个答案:

答案 0 :(得分: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", "someone@example.com"),
                    new XElement(shNs + "TelephoneNumber", "00357"),
                    new XElement(shNs + "ContactTypeOdemtofier", "IT Support")
            }));

        }
    }
}

答案 1 :(得分: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 = "someone@example.com",
                        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();