简体   繁体   中英

Adding Qualified NameSpace in XML using C#

How can I create XML given below in c#

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

Code snippet that i am using to generate the XML is this,

         [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.
            }

I am able to generate this XML using the above code

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

I am adding namespace using XmlSerializerNamespaces I am trying to consume SOAP Api and i have to pass the xml as request.

You can combine multiple namespaces by adding them to XML attributes:

[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; }
}

The namespace prefixes are provided to the XML serializer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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