【问题标题】:Linq to Xml and Namespace prefixesLinq to Xml 和命名空间前缀
【发布时间】: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


    【解决方案1】:

    在 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
    

    【讨论】:

      【解决方案2】:

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

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

      应该阅读

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

      【讨论】:

      • 欢迎来到 StackOverflow 安德鲁!您可以回答自己的问题,但是当您只是提供更多信息时,最好编辑您的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2011-01-21
      • 2012-05-27
      • 1970-01-01
      相关资源
      最近更新 更多