Linq to Xml 和命名空间前缀

发布于 2024-08-11 15:31:03 字数 1657 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2024-08-18 15:31:03

在 XElement 中的某个位置,您需要定义前缀。下面介绍了如何通过将 vt xmlns 放在顶部,并将其添加为 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 语句放在类/模块的顶部,高于其他所有内容):

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

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 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
荭秂 2024-08-18 15:31:03

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

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

应该读

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

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.

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

Should read

Dim vt as XNameSpace = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文