0

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.

Shub
  • 13
  • 5
  • 1
    [Possible duplicate](https://stackoverflow.com/questions/2339782/xml-serialization-and-namespace-prefixes) – Lennart Stoop May 30 '18 at 07:14
  • Lennart this is not what I want in my xml child node also have v11 namespace for eg: string string string string – Shub May 30 '18 at 07:21

1 Answers1

0

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);
Lennart Stoop
  • 1,649
  • 1
  • 12
  • 18