Hi.
I'm using a FileStream (instead of just the path to the xml file) to load an
XmlDocument. I'm doing this because I need to be able to prevent other
processes to update the file I'm working on. The problem is that I'm
getting multiple processing instructions and multiple root elements in my
xml file when I use a FileStream. Here is an example:
test_1.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="1">test</myTag>
</root>
test_2.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="2">test</myTag>
</root>
Public Sub test()
Dim fileStream As New FileStream("C:\Inetpub\wwwroot\test_1.xml",
FileMode.Open, FileAccess.ReadWrite, FileShare.None)
Dim xmlDocument As New XmlDocument()
Dim xmlDocument_2 As New XmlDocument()
Dim xmlNode As XmlNode
Dim xmlNodeNew As XmlNode
xmlDocument_2.Load("C:\Inetpub\wwwroot\test_2.xml" )
xmlNodeNew = xmlDocument_2.SelectSingleNode("root/myTag[@myValue='2']")
xmlDocument.Load(fileStream)
xmlNode = xmlDocument.ImportNode(xmlNodeNew.FirstChild, True)
xmlDocument.DocumentElement.AppendChild(xmlNode)
xmlDocument.Save(fileStream)
fileStream.Flush()
fileStream.Close()
End Sub
This is the result:
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="1">test</myTag>
</root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<myTag myValue="1">test</myTag>test
</root>
Does anyone know what I'm doing wrong?
Thanks,
Shawn