[RESOLVED] Write to existing XML (that has namespace)
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.
Re: Write to existing XML (that has namespace)
Here's a sample code to append a node to an xml file.
Code:
Dim doc As New XmlDocument()
Dim file As New FileStream("filename", FileMode.Open)
doc.Load(file)
Try
file.Close()
file.Dispose()
Dim root As XmlNode = doc.DocumentElement
Dim el As XmlElement = doc.CreateElement("Relationship")
el.SetAttribute("Id", "Test")
doc.DocumentElement.AppendChild(el)
root.InsertBefore(el, doc.DocumentElement.FirstChild)
doc.Save("filename")
Catch ex As Exception
Throw ex
End Try
Re: Write to existing XML (that has namespace)
Heya. Thanks for a solution. However, the node returns: <Relationship Id="test" xmlns="" />. It shouldn't add the "namespace".
Re: Write to existing XML (that has namespace)
Hi,
Replace this line:
Code:
Dim el As XmlElement = doc.CreateElement("Relationship")
with this:
Code:
Dim el As XmlElement = doc.CreateElement("Relationship", "http://schemas.openxmlformats.org/package/2006/relationships")
KG
Re: Write to existing XML (that has namespace)
Wouldn't that add the URI? Let me try.
EDIT: Yes, it adds the namespaceURI. I don't want that.
Here's my full code
Code:
Dim xdoc As New XmlDocument
xdoc.Load(xmlpath)
Dim root As XmlNode = xdoc.DocumentElement
Dim l As XmlElement = xdoc.CreateElement("Relationship")
l.SetAttribute("Id", vbNullString, relationid)
l.SetAttribute("Type", relationtype)
l.SetAttribute("Target", relationtarget)
xdoc.DocumentElement.AppendChild(l)
root.InsertBefore(l, xdoc.DocumentElement.FirstChild)
xdoc.Save(xmlpath)
Re: Write to existing XML (that has namespace)
Wait.... I didn't look proper. Seems all the links look-a-like when coding and staring after more then 10 hours...
Your solution works. +REP!
Re: [RESOLVED] Write to existing XML (that has namespace)