【发布时间】:2017-03-23 10:13:22
【问题描述】:
参考见: This Question
上面的要求是写
<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
<ci:field1>test</ci:field1>
<ca:field2>another test</ca:field2>
</root>
这里给出了代码
但我的要求是写
<myrootelement>
<ci:field1>test</ci:field1>
<ca:field2 ca:attrb1="value">another test</ca:field2>
</myrootelement>
因为这个元素将成为另一个大型 XML 的一部分,该 XML 包含自己的 ci 和 ca 命名空间引用。现在“<myrootelement>”作为子元素通过 api 插入到特定的 XElement,所以我无法访问 XSD 或命名空间等。
我尝试了什么
XElement element = new XElement("myrootelement",
new XElement(ci + "field1", "test"),
new XElement(ca + "field2", "another test", new XAttribute(ca + "attrb1", "value")));
但这会产生:
<myrootelement>
<field1 xmlns="http://somewhere.com">test</field1>
<field2 p2:attrb1="value" xmlns:p2="http://somewhereelse.com" xmlns="http://somewhereelse.com">another test</field2>
</myrootelement>
编辑:添加更多细节
然后使用
将上述元素发送到 Rest API{
.
.
var client = new RestClient(uri);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddParameter("text/xml", elem, ParameterType.RequestBody);
.
.
}
【问题讨论】:
标签: c# xml linq-to-xml xml-namespaces xmlwriter