Results 1 to 7 of 7

Thread: [RESOLVED] Write to existing XML (that has namespace)

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

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


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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)
    Last edited by Radjesh Klauke; Dec 17th, 2013 at 11:22 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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!


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  7. #7
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: [RESOLVED] Write to existing XML (that has namespace)

    Glad it helped you!
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width