Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2012 [RESOLVED] Write to existing XML (that has namespace)

$
0
0
Heya,

I'm trying to write (create a node) to an existing xml file wich has a namespace.

The following code gives "Object reference not set ....":

Code:

Dim xdoc As New XmlDocument                        '  write the values to the .rels.xml
xdoc.Load("myfile.xml")
xdoc.DocumentElement("Relationships").InnerText = "Relationship Id="test"....etc"    <--- Object reference not set
xdoc.save

The file:

Code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId3" Type="...." Target="docProps/app.xml"/>
  <Relationship Id="rId2" Type="..." Target="docProps/core.xml"/>
  <Relationship Id="rId1" Type="..." Target="xl/workbook.xml"/>
</Relationships>

I suppose I need to use the NameSpaceManager for this, but how to write to it so it looks like this:

Code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="test">  ' <---- my node/string to add
  <Relationship Id="rId3" Type="...." Target="docProps/app.xml"/>
  <Relationship Id="rId2" Type="..." Target="docProps/core.xml"/>
  <Relationship Id="rId1" Type="..." Target="xl/workbook.xml"/>
</Relationships>

Code:

Dim xdoc As New XmlDocument
xdoc.Load(myxml)

Dim mynsman As New XmlNamespaceManager(xdoc.NameTable)
mynsman.AddNamespace("Relationships", "http://schemas.openxmlformats.org/package/2006/relationships")
'  now I don't know what to do next....

Thanks for the help in advance.

Viewing all articles
Browse latest Browse all 27554

Trending Articles