remove root node xml - vb.net

hi i'm doing some work in vb.net and want to remove the root node in an XML file making the next node the root,

I know i could use text reader and remove the first and last lines but if possible i would like to do this without using a text reader.

if you see the xml example below i wish to remove the <import> node i've been trying various things but can only remove child nodes.
<import>
  <cXML xml:lang="en" ID="1234" timestamp="2010-04-03T16:11:04">
    <Header>
      <From>
        <Credential domain="MyOrganisation">
          <Identity>123456</Identity>
        </Credential>
      </From>
    </Header>
  </cXML>
</import>

Open in new window

IanPaskinAsked:
Who is Participating?
 
Hawkvalley1Commented:
Since your file probably already exists:

   Dim xl As New XmlDocument
    xl.Load(C:\yourfile.xml")
     
    Dim xmls As New  XmlDocument
    'copy only the inner part
     xmls.LoadXml(xl.DocumentElement.InnerXml)

    'save to the  same file all done =)
    xmls.Save("C:\youfile.xml")

   'dispose of cached memory
   xl = Nothing
   xmls = Nothing
0
 
lazyberezovskyCommented:
Easiest way:
Regex.Replace(xmlText, "</?import>", String.Empty)

I hope you know how to read xml from file to string xmlText?
0
 
Hawkvalley1Commented:
   'make initial file
    Dim str As String = "<import>"
    str = String.Concat(str, Environment.NewLine, "<cXML xml:lang=""en"" ID=""1234"" timestamp=""2010-04-03T16:11:04"" >")
    str = String.Concat(str, Environment.NewLine, "     ", "<Header>")
    str = String.Concat(str, Environment.NewLine, "     ", "<From>")
    str = String.Concat(str, Environment.NewLine, "     ", "     ", "<Credential domain=""MyOrganisation"" >")
    str = String.Concat(str, Environment.NewLine, "     ", "     ", "     ", "<Identity>123456</Identity>")
    str = String.Concat(str, Environment.NewLine, "     ", "     ", "</Credential>")
    str = String.Concat(str, Environment.NewLine, "     ", "</From>")
    str = String.Concat(str, Environment.NewLine, "     ", "</Header>")
    str = String.Concat(str, Environment.NewLine, "     ", "</cXML>")
    str = String.Concat(str, "</import>")
    Dim xl As New XmlDocument
    xl.LoadXml(str)
    xl.Save("C:\Test\EExml.xml")

    Dim xmls As New XmlDocument
    'copy only the inner part
    xmls.LoadXml(xl.DocumentElement.InnerXml)
    'save to the same file all done =)
    xmls.Save("C:\Test\EExml.xml")
0
 
IanPaskinAuthor Commented:
perect thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.