【问题标题】:How to control the namespace prefix in XML?如何控制 XML 中的命名空间前缀?
【发布时间】:2014-08-29 05:36:23
【问题描述】:

我想创建一个 XML,该 XML 将与请求一起发送到第 3 方站点以创建会议参与者。

文档位于: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>johnsmith@xyz.com</email>
        </securityContext>
    </header>
    <body>
        <bodyContent xsi:type=
            "java:com.webex.service.binding.attendee.CreateMeetingAttendee">
            <person>
                <name>alterhost</name>
                <address>
                    <addressType>PERSONAL</addressType>
                </address>
                <email>host1@test.com</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", "xyz@domain.com")),
                    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>xyz@domain.com</email>
      </person>
      <sessionKey>###</sessionKey>
    </bodyContent>
  </body>
</serv:message>

如您所见,它与请求 XML 格式不匹配。

问题:

  1. 从顶部看,&lt;?xml version="1.0"?&gt; 不见了。
  2. &lt;serv:message xmlns:serv=... 应该是 &lt;serv:message xmlns:xsi=...
  3. &lt;bodyContent xsitype="..."&gt; 应该是 &lt;bodyContent xsi:type="..."&gt;

我已经通过http://msdn.microsoft.com/en-us/library/bb387075.aspx但无法更正它。

谁能帮我解决这个问题。非常感谢任何帮助。

【问题讨论】:

  • 您显示为“示例 XML”的文本不是有效的 XML(具有未定义的前缀“serv:”)。您将无法使用产生有效 XML 的类型(如 XDocument)来构造此类文本。
  • 你是如何写出 XML 的?
  • @AlexeiLevenkov 是的,你是对的。我认为&lt;serv:message xmlns:serv="http://www.w3.org/2001/XMLSchema-instance"&gt; 会起作用吗?
  • @Mrchief:我正在通过 root.ToString() 测试 XML 输出。
  • 当你解决完这个问题后,对思科大喊大叫,因为他们在其中一个示例中提供了无效的 XML。

标签: c# xml


【解决方案1】:
  1. 您需要使用XDeclaration 对象

  2. xmlns:xsi 添加另一个XAttribute,类似于为xmlns:serv 所做的操作

  3. 使用附加字符串"type"xsi变量产生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", "xyz@domain.com")),
        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>xyz@domain.com</email>
         </person>
         <sessionKey>###</sessionKey>
      </bodyContent>
   </body>
</serv:message>

PS:XDocument.ToString() 不打印 xml 声明行,但 XDocument.Save() 在保存的 XML 文件中包含声明行。与此事相关的帖子:XDocument.ToString() drops XML Encoding Tag

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多