【发布时间】:2012-11-28 20:34:34
【问题描述】:
给定以下XDocument,初始化为变量xDoc:
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
<ReportSection>
<Width />
<Page>
</ReportSections>
</Report>
我有一个嵌入在 XML 文件中的模板(我们称之为body.xml):
<Body xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
<ReportItems />
<Height />
<Style />
</Body>
我想把它作为<ReportSection> 的孩子。问题是如果通过XElement.Parse(body.xml) 添加它,它会保留命名空间,即使我认为应该删除命名空间(复制自身没有意义 - 已经在父级上声明)。如果我不指定命名空间,它会放置一个空的命名空间,所以它变成<Body xmlns="">。
有没有办法将XElement 正确合并到XDocument 中?我想在xDoc.Root.Element("ReportSection").AddFirst(XElement)之后得到以下输出:
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
<ReportSection>
<Body>
<ReportItems />
<Height />
<Style />
</Body>
<Width />
<Page>
</ReportSections>
</Report>
【问题讨论】:
-
@Pheonixblade9:不幸的是,在我的例子中,XML 不是由代码创建的,因为它是在 StackOverflow 上的几十个示例中完成的,包括你提到的那个。从头开始创建 XElement 并将其合并到 XDocument 中没有问题。这是关于 XElement.Parse 返回一个节点树。
标签: c# xml vb.net linq-to-xml xelement