简体   繁体   English

C# XmlSerializer 在子节点中定义带前缀的命名空间

[英]C# XmlSerializer define namespace with prefix in a child node

I need to create an XML document with C# that is like this:我需要使用 C# 创建一个 XML 文档,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Container>
  <Info>
    <request xmlns:a="http://www.UKMail.com/Services/Contracts/DataContracts">
      <a:AuthenticationToken>token</a:AuthenticationToken>
      <a:Username>username</a:Username>
      <a:ConsignmentNumber>12345</a:ConsignmentNumber>
    </request>
  </Info>
</Container>

The critical part is the namespace definition with a prefix (xmlns:a=...) is in a child node, not the root node.关键部分是带有前缀 (xmlns:a=...) 的命名空间定义位于子节点中,而不是根节点中。 I have only been able to produce this document so far:到目前为止,我只能出示这份文件:

<?xml version="1.0" encoding="utf-8"?>
<Container xmlns:a="http://www.UKMail.com/Services/Contracts/DataContracts">
  <Info>
    <a:request>
      <a:AuthenticationToken>token</a:AuthenticationToken>
      <a:Username>username</a:Username>
      <a:ConsignmentNumber>12345</a:ConsignmentNumber>
    </a:request>
  </Info>
</Container>

This is rejected by the web service - if you move the xmlns:a.. part to the request node the web service is happy with it.这被 web 服务拒绝 - 如果您将 xmlns:a.. 部分移动到请求节点,web 服务会对此感到满意。

This is how I am generating the XML at the moment:这就是我现在生成 XML 的方式:

class Program
    {
        static void Main(string[] args)
        {
            SerializeObject("XmlNamespaces.xml");
        }

        public static void SerializeObject(string filename)
        {
            var mySerializer = new XmlSerializer(typeof(Container));
            // Writing a file requires a TextWriter.
            TextWriter myWriter = new StreamWriter(filename);

            // Creates an XmlSerializerNamespaces and adds two
            // prefix-namespace pairs.
            var myNamespaces = new XmlSerializerNamespaces();
            myNamespaces.Add("a", "http://www.UKMail.com/Services/Contracts/DataContracts");


            Container container = new Container
            {
                Info = new CancelConsignmentRequest
                {
                    request = new CancelConsignmentRequestInfo
                    {
                        AuthenticationToken = "token",
                        ConsignmentNumber = "12345",
                        Username = "username"
                    }
                }
            };

            mySerializer.Serialize(myWriter, container, myNamespaces);
            myWriter.Close();
        }
    }

    public class Container
    {
        public CancelConsignmentRequest Info { get; set; } = new CancelConsignmentRequest();
    }

    [XmlRoot(Namespace = "http://www.UKMail.com/Services/Contracts/ServiceContracts")]
    public class CancelConsignmentRequest
    {
        [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts")]
        public CancelConsignmentRequestInfo request { get; set; } = new CancelConsignmentRequestInfo();
    }

    public class CancelConsignmentRequestInfo
    {
        [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts", Order = 0)]
        public string AuthenticationToken { get; set; }
        [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts", Order = 1)]
        public string Username { get; set; }

        [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts", Order = 2)]
        public string ConsignmentNumber { get; set; }
    }

I have not been able to work out how to place the namespace definition with a prefix in one of the child nodes.我无法弄清楚如何将带有前缀的名称空间定义放置在其中一个子节点中。 Does anyone know how to do this in C# please?有谁知道如何在 C# 中执行此操作吗? Thanks.谢谢。

This is possible.这个有可能。 The code below does what you are asking for.下面的代码可以满足您的要求。

class Program
{
    static void Main(string[] args)
    {
        SerializeObject("XmlNamespaces.xml");
    }

    public static void SerializeObject(string filename)
    {
        var mySerializer = new XmlSerializer(typeof(Container));
        // Writing a file requires a TextWriter.
        TextWriter myWriter = new StreamWriter(filename);

        // Creates an XmlSerializerNamespaces and adds two
        // prefix-namespace pairs.
        var myNamespaces = new XmlSerializerNamespaces();
        //myNamespaces.Add("a", "http://www.UKMail.com/Services/Contracts/DataContracts");

        Container container = new Container
        {
            Info = new CancelConsignmentRequest
            {
                request = new CancelConsignmentRequestInfo
                {
                    AuthenticationToken = "token",
                    ConsignmentNumber = "12345",
                    Username = "username"
                }
            }
        };

        mySerializer.Serialize(myWriter, container, myNamespaces);
        myWriter.Close();
    }
}

public class Container
{
    public CancelConsignmentRequest Info { get; set; } = new CancelConsignmentRequest();
}

public class CancelConsignmentRequest
{
    public CancelConsignmentRequestInfo request { get; set; } = new CancelConsignmentRequestInfo();
}

[XmlRoot(Namespace = "http://www.UKMail.com/Services/Contracts/ServiceContracts")]
public class CancelConsignmentRequestInfo
{
    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(
        new[] { new XmlQualifiedName("a", "http://www.UKMail.com/Services/Contracts/DataContracts"), });
    [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts", Order = 0)]
    public string AuthenticationToken { get; set; }
    [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts", Order = 1)]
    public string Username { get; set; }

    [XmlElement(Namespace = "http://www.UKMail.com/Services/Contracts/DataContracts", Order = 2)]
    public string ConsignmentNumber { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM