2014-08-29 90 views
2

我想創建一個XML,它將發送一個請求到第三方站點來創建會議參加者。如何在XML中控制名稱空間前綴?

的文檔是:https://developer.cisco.com/media/webex-xml-api/121CreateMeetingAttendee.html

給出有實例顯示請求XML應該在這個格式:

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

直到現在我都試過:

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", "###")))); 

它的結果以下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> 

正如您所看到的,它與請求XML格式不匹配。

問題:

  1. 從頂部<?xml version="1.0"?>丟失。
  2. <serv:message xmlns:serv=...應該<serv:message xmlns:xsi=...
  3. <bodyContent xsitype="...">應該<bodyContent xsi:type="...">

我已經通過了http://msdn.microsoft.com/en-us/library/bb387075.aspx,但不能糾正。

任何人都可以幫助我解決這個問題。任何幫助,高度讚賞。

+2

您顯示爲「示例XML」的文本不是有效的XML(具有未定義的前綴「serv:」)。您將無法使用生成像XDocument這樣的有效XML的類型來構造這樣的文本。 – 2014-08-29 06:00:21

+0

你是如何編寫XML的? – Mrchief 2014-08-29 06:19:43

+0

@AlexeiLevenkov是的,你是對的。我認爲''會工作嗎? – 2014-08-29 06:31:33

回答

1
  1. 您需要使用XDeclaration對象

  2. 添加另一個XAttributexmlns:xsi類似於你做了什麼xmlns:serv

  3. 使用xsi變量與字符串"type"追加到生產xsi:type屬性

完整的示例(從您發佈的代碼修改):

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

控制檯輸出:

<?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()不打印XML報關行,但XDocument.Save()包括保存XML聲明行文件。與此相關的主題:XDocument.ToString() drops XML Encoding Tag