Linq to Xml and Namespace Prefixes

I am working with Linq to Xml to manipulate openXml documents. More precisely, I am trying to read and write to the user properties of documents. I am currently having a problem adding a prefix to XElement. My code looks like this:

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 contained in the details before adding:

<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" />

Xml after calling props.add () method:

   <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>

Inside the property element, I should get

<vt:lpwstr>test value</vt:lpwstr> 

but just can't do it. I do not need the xmlns attribute for this element. I think I somehow need to return the vt XNameSpace map back to the namespace declaration in the root "Properties" element. Anyone have any suggestions?

+4
source share
3 answers

- 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- , vt, XML . XML- ( 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
+1

, , , Xml Literals. , . , , Office .

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

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

All Articles