【发布时间】:2016-05-22 23:58:20
【问题描述】:
我有一段代码可以使用这种 Web 服务方法提取 XML 文档类型
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XAttribute attribute = new XAttribute(xsi + "type", "xsd:string");
XElement node_user_id = new XElement("user_id", attribute, user.code);
XDocument doc = new XDocument(new XElement("ranzcp_user", new XAttribute(XNamespace.Xmlns + "ns1", "urn:logon"), node_user_id));
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(elem.ToString());
使用上面的代码,我能够完全像这样提取一个 xml 文档:
<ranzcp_user xmlns:ns1="urn:logon">
<user_id xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" p3:type="xsd:string">12345678</user_id>
</ranzcp_user>
但我真正需要的是这个:
<ranzcp_user xmlns:ns1="urn:logon">
<user_id xsi:type="xsd:string">12345678</user_id>
</ranzcp_user>
有什么方法可以得到我需要的 xml 格式,第二个 xsi:type="xsd:string" 属性在解析 xml 数据时是否必要?
TIA!
【问题讨论】:
-
为什么要删除命名空间?你有错误吗?谁在生成 xml?如果使用模式的命名空间出现错误,则 xml 无效,需要修复。删除命名空间是个杂事。
-
@under_score
xsi:type没有声明xsi前缀 (xmlns:xsi="...") 会导致不符合 XML 规范的内容(因此不符合 XML 的条件,反过来可以'不被标准 XML 解析器生成/处理) -
同意@har07。有关详细信息,请参阅How to restrict the value of an XML element using xsi:type in XSD?
-
感谢所有输入,现在它只是我需要从我的 Web 服务返回的 XML 的规范,只是不知道一旦我把它交给他们,对方将如何解析它
标签: c# xml web-services