【问题标题】:Set namespace to XElement将命名空间设置为 XElement
【发布时间】:2014-12-31 06:39:31
【问题描述】:
我在 XML 树的 XElemenet 中得到一个空的 xmlns="" 属性。
当我将其命名空间设置为文档命名空间时,如下所示:
string xmlns="FreeForm/SchemaDescription";
XNamespace ana = xmlns;
XElement interactiveRootTag = new XElement(ana + "InteractiveRootTag");
空的xmlns="" 不再存在,但是这个XElement 的所有子元素,得到空的xmlns=""。
有什么想法吗?
【问题讨论】:
标签:
c#
xml
namespaces
xml-namespaces
【解决方案1】:
你必须以这种方式添加子元素:
string xmlns="FreeForm/SchemaDescription";
XNamespace ana = xmlns;
XElement interactiveRootTag = new XElement(ana + "InteractiveRootTag");
interactiveRootTag.Add(new XElement(ana + "ChildElement",
new XAttribute("attribute","AttributeValue")));
像这样获取 XML
<InteractiveRootTag xmlns="FreeForm/SchemaDescription">
<ChildElement attribute="AttributeValue" />
</InteractiveRootTag>