我想创建一个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>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格式不匹配。
问题:
<?xml version="1.0"?>
丢失。<serv:message xmlns:serv=...
应为<serv:message xmlns:xsi=...
<bodyContent xsitype="...">
应为<bodyContent xsi:type="...">
我已经完成http://msdn.microsoft.com/en-us/library/bb387075.aspx但无法纠正。
任何人都可以帮我解决这个问题。任何帮助都非常感谢。
答案 0 :(得分:1)
您需要使用XDeclaration
对象
为XAttribute
添加另一个xmlns:xsi
,类似于您对xmlns:serv
所做的
使用附加字符串xsi
的{{1}}变量生成"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", "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());
PS:<?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>
不打印xml声明行,但XDocument.ToString()
包含已保存XML文件中的声明行。与此事相关的主题:XDocument.ToString() drops XML Encoding Tag