Linq to Xml和命名空间前缀 [英] Linq to Xml and Namespace prefixes
问题描述
我正在使用Linq to Xml来处理openXml文档.更准确地说,我试图读取和写入文档自定义属性.我目前在将前缀附加到XElement时遇到问题.我的代码如下:
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")) _
)
添加前道具中包含的Xml是:
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" />
props.add method()调用后的Xml是:
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>
在property元素中,我应该得到
Within the property element I should be getting
<vt:lpwstr>test value</vt:lpwstr>
但是无法做到这一点.我也不想在此元素的xmlns属性.我想我需要以某种方式将vt XNameSpace映射返回到根元素"Properties"中的名称空间声明.有人有什么建议吗?
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?
推荐答案
在XElement中的某个位置,您需要定义前缀.通过将vt
xmlns放在顶部,将其添加为XAttribute的方法如下:New XAttribute(XNamespace.Xmlns + "vt", "http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes")
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")
Dim main As XNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
Dim vt As XNamespace = "http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes"
Dim formatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
Dim pid = "2"
Dim props As New XElement(main + "Properties", New XAttribute(XNamespace.Xmlns + "vt", "http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes"))
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"))
XML文字和全局名称空间可能更容易,但是您仍然需要父级XML中列出的vt
.这是一个XML Literals示例(请记住,将两个Imports语句都放在类/模块的顶部,然后放在其他所有位置):
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):
Imports <xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties">
Imports <xmlns:vt="http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes">
Sub GetXml()
Dim formatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
Dim pid = "2"
Dim props2 = <Properties>
<property fmtid=<%= formatId %> pid=<%= pid + 1 %> name="test">
<vt:lpwstr>test value</vt:lpwstr>
</property>
</Properties>
MsgBox(props2.ToString)
End Sub
这篇关于Linq to Xml和命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!