Linq to Xml 和命名空间前缀
我正在使用 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 是:
<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 映射回根元素“Properties”中的名称空间声明。有人有什么建议吗?
I am working with Linq to Xml to manipulate openXml documents. More precisely I am trying to read and write to the documents custom properties. I am currently having a problem appending a prefix onto an XElement. My code looks like:
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")) _
)
The Xml contained in props before the add is :
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" />
The Xml after the props.add method() call is:
<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>
Within the property element I should be getting
<vt:lpwstr>test value</vt:lpwstr>
but just can't get this to happen. I don't want the xmlns attribute for this element here either. I think I somehow need to get the map the vt XNameSpace back to the namespace declaration in the root element "Properties". Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 XElement 中的某个位置,您需要定义前缀。下面介绍了如何通过将
vt
xmlns 放在顶部,并将其添加为 XAttribute:New XAttribute(XNamespace.Xmlns + "vt", "http://schemas.openxmlformats .org/officeDocument2006/docPropsVTypes")
XML 文字和全局命名空间可能更容易,但您仍然需要在父级别的 XML 中列出
vt
。这是一个 XML Literals 示例(请记住将两个 Imports 语句放在类/模块的顶部,高于其他所有内容):At some place in the XElement, you'll need the prefix defined. Here's how to do it by putting the
vt
xmlns at the top, by adding it as an XAttribute:New XAttribute(XNamespace.Xmlns + "vt", "http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes")
XML Literals and global namespaces may be easier, but you'll still need
vt
listed in the XML at a parent level. Here's an XML Literals example (remember to put both Imports statements at the top of the class/module, above everything else):我发现控制命名空间声明位置的方法是使用 Xml Literals。我还必须从头开始重新创建文档,并将旧文档中的任何现有信息复制到新创建的文档中,这并不理想。上面的示例中还存在一个错误,该错误足以在运行代码后损坏任何 Office 文档。
应该读
The way I have found to control where the namepaces are declared is to use Xml Literals. I also have to recreate the document from scratch and copy any existing information from the old document into my newly created document which isn't ideal. There is also a bug in the example above which is enough to get any of the Office Documents to corrupt after running the code.
Should read