Linq到Xml和名称空间前缀

Linq到Xml和名称空间前缀,xml,linq,linq-to-xml,Xml,Linq,Linq To Xml,我正在使用LINQtoXML来操作openXml文档。更确切地说,我正在尝试读取和写入文档的自定义属性。我当前在XElement上添加前缀时遇到问题。我的代码如下所示: Dim main as XNameSpace = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" Dim vt as XNameSpace = "http://schemas.openxmlformats.org/officeD

我正在使用LINQtoXML来操作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为:


测试值
在属性元素中,我应该得到

<vt:lpwstr>test value</vt:lpwstr> 
测试值

但就是不能让这一切发生。我也不想在这里使用这个元素的xmlns属性。我想我需要将vt XNameSpace映射回根元素“Properties”中的名称空间声明。有人有什么建议吗?

我发现控制名称空间声明位置的方法是使用Xml文本。我还必须从头开始重新创建文档,并将旧文档中的任何现有信息复制到新创建的文档中,这并不理想。上面的示例中还有一个bug,它足以使任何Office文档在运行代码后损坏

Dim vt as XNameSpace = "http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes"
应该读

Dim vt as XNameSpace = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"

在XElement中的某个位置,需要定义前缀。下面介绍如何将
vt
xmlns放在顶部,并将其作为XAttribute添加:
newXattribute(XNamespace.xmlns+“vt”http://schemas.openxmlformats.org/officeDocument2006/docPropsVTypes“”

XML文本和全局名称空间可能更容易,但您仍然需要在XML中的父级列出
vt
。下面是一个XML文本示例(请记住将两个Imports语句放在类/模块的顶部,高于其他所有语句):

导入
进口
子GetXml()
Dim formatId=“{D5CD505-2E9C-101B-9397-08002B2CF9AE}”
Dim pid=“2”
Dim props2=
测试值
MsgBox(props2.ToString)
端接头

欢迎来到StackOverflow Andrew!您可以回答自己的问题,但当您只是提供更多信息时,最好编辑您的问题。
Dim vt as XNameSpace = "http://schemas.openxmlformats.org/officeDocument/2006/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"))
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