Dukhabandhu Sahoo
Dukhabandhu Sahoo

Reputation: 1424

How to control the namespace prefix in XML?

I want to create an XML which will be send with a request to a 3rd party site to Create Meeting Attendee.

The documentation is at: https://developer.cisco.com/media/webex-xml-api/121CreateMeetingAttendee.html

The example given there shows the request XML should be in this format:

<?xml version="1.0"?>
<serv:message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <header>
        <securityContext>
            <webExID>hostid</webExID>
            <password>hostpassword</password>
            <siteID>0000</siteID>
            <partnerID>9999</partnerID>
            <email>[email protected]</email>
        </securityContext>
    </header>
    <body>
        <bodyContent xsi:type=
            "java:com.webex.service.binding.attendee.CreateMeetingAttendee">
            <person>
                <name>alterhost</name>
                <address>
                    <addressType>PERSONAL</addressType>
                </address>
                <email>[email protected]</email>
                <type>MEMBER</type>
            </person>
            <role>HOST</role>
            <sessionKey>808961063</sessionKey>
        </bodyContent>
    </body>
</serv:message>

Till now I have tried:

XNamespace aw = "http://www.w3.org/2001/XMLSchema-instance";
            XNamespace xsi = "java:com.tempService";

            XElement root = new XElement(aw + "message",
                new XAttribute(XNamespace.Xmlns + "serv", aw),
                new XElement("header",
                    new XElement("securityContext", new XElement("siteID", "123"),
                        new XElement("partnerID", "111"))),

                new XElement("body", new XElement("bodyContent",
                    new XAttribute("xsitype", xsi),
                    new XElement("person", new XElement("name", "sample content"),
                        new XElement("email", "[email protected]")),
                    new XElement("sessionKey", "###"))));

It results the following XML:

<serv:message xmlns:serv="http://www.w3.org/2001/XMLSchema-instance">
  <header>
    <securityContext>
      <siteID>123</siteID>
      <partnerID>111</partnerID>
    </securityContext>
  </header>
  <body>
    <bodyContent xsitype="java:com.tempService">
      <person>
        <name>sample content</name>
        <email>[email protected]</email>
      </person>
      <sessionKey>###</sessionKey>
    </bodyContent>
  </body>
</serv:message>

As you can see it does not match with the request XML format.

Problems:

  1. From the top <?xml version="1.0"?> is missing.
  2. <serv:message xmlns:serv=... should be <serv:message xmlns:xsi=...
  3. <bodyContent xsitype="..."> should be <bodyContent xsi:type="...">

I have gone through http://msdn.microsoft.com/en-us/library/bb387075.aspx but could not correct it.

Can any one help me here to resolve this problem. Any help is highly appreciated.

Upvotes: 2

Views: 577

Answers (1)

har07
har07

Reputation: 89285

  1. You need to use an XDeclaration object

  2. Add another XAttribute for xmlns:xsi similar to what you did for xmlns:serv

  3. Use xsi variable appended with string "type" to produce xsi:type attribute

Complete example (modified from the code you posted) :

XNamespace aw = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsi = "java:com.tempService";

XElement root = new XElement(aw + "message",
    new XAttribute(XNamespace.Xmlns + "serv", aw),
    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
    new XElement("header",
        new XElement("securityContext", new XElement("siteID", "123"),
            new XElement("partnerID", "111"))),

    new XElement("body", new XElement("bodyContent",
        new XAttribute(xsi + "type", "java:com.webex.service.binding.attendee.CreateMeetingAttendee"),
        new XElement("person", new XElement("name", "sample content"),
            new XElement("email", "[email protected]")),
        new XElement("sessionKey", "###"))));
//use XDocument with XDeclaration to produce XML including xml declaration line :
var doc = new XDocument(new XDeclaration("1.0", null, null), root);
Console.WriteLine(doc.Declaration + Environment.NewLine + doc.ToString());

Console Output :

<?xml version="1.0"?>
<serv:message xmlns:serv="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="
   java:com.tempService">
   <header>
      <securityContext>
         <siteID>123</siteID>
         <partnerID>111</partnerID>
      </securityContext>
   </header>
   <body>
      <bodyContent xsi:type="java:com.webex.service.binding.attendee.CreateMeeting
         Attendee">
         <person>
            <name>sample content</name>
            <email>[email protected]</email>
         </person>
         <sessionKey>###</sessionKey>
      </bodyContent>
   </body>
</serv:message>

PS: XDocument.ToString() doesn't print xml declaration line, but XDocument.Save() includes declaration line in the saved XML file. Thread related to this matter : XDocument.ToString() drops XML Encoding Tag

Upvotes: 1

Related Questions