【发布时间】:2009-11-17 10:45:39
【问题描述】:
我正在使用 Linq to Xml 来操作 openXml 文档。更准确地说,我正在尝试读取和写入文档的自定义属性。我目前在将前缀附加到 XElement 时遇到问题。我的代码如下:
Dim main as XNameSpace = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
Dim vt as XNameSpace = "http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes"
Dim props as XElement = cXDoc.Element(main + "Properties"
props.Add(New XElement(main + "property"), _
New XAttribute("fmtid", formatId), _
New XAttribute("pid", pid + 1), _
New XAttribute("name", "test"), _
New XElement(vt + "lpwstr", "test value")) _
)
props中包含的xml在add之前是:
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" />
props.add method() 调用后的xml是:
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="test">
<lpwstr xmlns="http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes">test value</lpwstr>
</property>
</Properties>
在属性元素中我应该得到
<vt:lpwstr>test value</vt:lpwstr>
但就是无法做到这一点。我也不想要这个元素的 xmlns 属性。我想我需要以某种方式将 vt XNameSpace 映射回根元素“属性”中的命名空间声明。有人有什么建议吗?
【问题讨论】:
标签: xml linq linq-to-xml