c# - 将命名空间设置为 XElement

标签 c# xml namespaces xml-namespaces

我得到一个空 xmlns="" XML 树中 XElemenet 中的属性。
当我将其命名空间设置为文档命名空间时,如下所示:

string xmlns="FreeForm/SchemaDescription";
XNamespace ana = xmlns;
XElement interactiveRootTag = new XElement(ana + "InteractiveRootTag");

xmlns=""不再存在,但是这个 XElement 的所有子元素,得到空 xmlns="" .

有任何想法吗?

最佳答案

您必须以这种方式添加子元素:

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>

关于c# - 将命名空间设置为 XElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26734032/

相关文章:

C++ 不使用 使用

php - 类是否只能对其命名空间可见

c# - .net native 用于桌面应用程序

c# - 自定义密码文本框 C# Windows 应用程序

c# - 为 Unity3D 构建一个正交相机,我怎样才能让四个特定目标保持在视线范围内?

html - 常规 : parsing xml with HTML tags inside

c# - 将图像传递给 openCV 中的 blur() 或 Canny() 时出现类似幽灵的伪像

java - 文档元素后的 Android java XML 垃圾

php - 在 iPhone 上获取数据

php - 在 PHP 中实现数据结构的最佳方法?