使用C#

时间:2018-05-30 07:11:44

标签: c# asp.net .net xml

如何在c#

中创建下面给出的XML
  <v1:Request ProductId="string" xmlns:v1="com/user/service/core/services/v1" xmlns:v11="com/user/ds/sch/pii/v1" xmlns:enum="com/uaer/schema/database/tcps/enumerations">
      <v1:Name>string</v1:Name>
      <v1:ID>string</v1:ID>
      <v1:Code>string</v1:Code>
      <v1:Key>string</v1:Key>
      <v1:RequestKey>string</v1:RequestKey>
      <v1:PartnerId>string</v1:PartnerId>
      <v1:CustomerInfo>
        <v1:Name>
          <v11:Title>string</v11:Title>
          <v11:Forename>string</v11:Forename>
          <v11:Surname>string</v11:Surname>
          <v11:Suffix>string</v11:Suffix>
        </v1:Name>
        <v1:Number>
          <v11:Name>string</v11:Name>
          <v11:Id>string</v11:Id>
        </v1:Number>
    </v1:Request>

我用来生成XML的代码片段就是这个,

         [XmlRoot("Request")]
            public class Request{
            [XmlAttribute]
            public string ProductId{get;set;}
            [XmlElement("Name")]
            public string Name{get;set;}
            [XmlElement("ID")]
            public string ID{get;set;}
            [XmlElement("Code")]
            public string Code{get;set;}
            [XmlElement("Key")]
            public string Key{get;set;}
            [XmlElement("RequestKey")]
            public string RequestKey{get;set;}
            [XmlElement("PartnerId")]
            public string PartnerId{get;set;}
            [XmlElement("CustomerInfo")]
            public CustomerInfo CustomerInfo= new CustomerInfo();
//this(CustomerInfo) is a class which contains the reference of NAME and NUMBER class.
    //NAME and NUMBER class contains their respective properties.
            }

我可以使用上面的代码生成这个XML

<Request ProductId="string" xmlns:v1="com/user/service/core/services/v1" xmlns:v11="com/user/ds/sch/pii/v1" xmlns:enum="com/uaer/schema/database/tcps/enumerations">
          <Name>string</Name>
          <ID>string</ID>
          <Code>string</Code>
          <Key>string</Key>
          <RequestKey>string</RequestKey>
          <PartnerId>string</PartnerId>
          <CustomerInfo>
            <Name>
              <Title>string</Title>
              <Forename>string</Forename>
              <Surname>string</Surname>
              <Suffix>string</Suffix>
            </Name>
            <Number>
              <Name>string</Name>
              <Id>string</Id>
            </Number>
        </Request>

我正在使用XmlSerializerNamespaces添加命名空间 我正在尝试使用SOAP Api,我必须将xml作为请求传递。

1 个答案:

答案 0 :(得分:0)

您可以通过将多个命名空间添加到XML属性来组合它们:

[XmlRoot("Request", Namespace = "com/user/service/core/services/v1")]
public class Request
{
  [XmlAttribute]
  public string ProductId { get; set; }
  [XmlElement("Name")]
  public string Name { get; set; }

  [XmlElement("CustomerInfo")]
  public CustomerInfo CustomerInfo = new CustomerInfo();
}

public class CustomerInfo
{
  [XmlElement("Name")]
  public Name Name { get; set; }
}

public class Name
{
  [XmlElement("Title", Namespace = "com/user/ds/sch/pii/v1")]
  public string Title { get; set; }
}

名称空间前缀提供给XML序列化程序:

  XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  ns.Add("v1", "com/user/service/core/services/v1");
  ns.Add("v11", "com/user/ds/sch/pii/v1");
  XmlSerializer xser = new XmlSerializer(typeof(Request));
  xser.Serialize(Console.Out, new Request(), ns);