Updating contents of XML node

  • Thread starter Web Team @ Borough of Poole
  • Start date
W

Web Team @ Borough of Poole

Hi All,

I'm trying to update the contents of an XML node. My original code:

Dim loXMLDoc As XmlDocument = New XmlDocument
Dim loNode As XmlNode
loXMLDoc.Load(HttpContext.Current.Server.MapPath(strXMLFileLocation))
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea" &
textAreaID)
loNode.InnerText = strContent
loXMLDoc.Save(HttpContext.Current.Server.MapPath(strXMLFileLocation))
loNode = Nothing
loXMLDoc = Nothing

Causes this error:
"There is no Unicode byte order mark. Cannot switch to Unicode."

I Googled the error and found that a work around is to read the file in
via a streamreader, then open the XML doc from the stream. So, my new
code is:

Dim loXMLDoc As XmlDocument = New XmlDocument
Dim loNode As XmlNode
Dim objStreamReader As StreamReader
objStreamReader =
File.OpenText(HttpContext.Current.Server.MapPath(strXMLFileLocation))
loXMLDoc.Load(objStreamReader)
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea" &
textAreaID)
loNode.InnerText = strContent
objStreamReader.Close()
objStreamReader = Nothing
loXMLDoc.Save(HttpContext.Current.Server.MapPath(strXMLFileLocation))
loNode = Nothing
loXMLDoc = Nothing

Which causes a different error:
"The process cannot access the file "<Physical path to my XML file>"
because it is being used by another process."

Any help/suggestions will be much appreciated!

Thanks,
Simon.
 
G

Guest

Change back to your origional code, and do this before running:

- Open your XML file in Notepad
- Make sure your first line looks like this:
<?xml version="1.0" encoding="utf-8"?>
- Go to Save As...
- Change the Encoding option to UTF-8 and click save to save it.

Hope that helps.
 
W

Web Team @ Borough of Poole

Hi Andy,

Thanks for your post, I did as you said with the XML file, and my code
now reads:

Dim loXMLDoc As XmlDocument = New XmlDocument
Dim loNode As XmlNode

loXMLDoc.Load(HttpContext.Current.Server.MapPath(strXMLFileLocation))
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea"
& textAreaID)
loNode.InnerText = strContent

loXMLDoc.Save(HttpContext.Current.Server.MapPath(strXMLFileLocation))
loNode = Nothing
loXMLDoc = Nothing

Looks like we've solved the UTF8 error :) But I am still getting this
one, which I assumed was due to using the streamreader, but perhaps
not?

"The process cannot access the file "<Path to my XML doc>" because it
is being used by another process"

Any more help anyone may be able to offer will be appreciated.

Regards,

Simon.
 
G

Guest

I converted your code into my own C# example (b/c I don't really know VB),
and here's what I got:

XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("/TestWebApp/XMLFile1.xml"));
XmlNode node = doc.SelectSingleNode("/ReportDef");
node.InnerText = "hello world";
doc.Save(HttpContext.Current.Server.MapPath("/TestWebApp/XMLFile1.xml"));
node = null;
doc = null;

First, I got an error that I expected: "Access denied". By default, files in
web accessable directories don't have write permission granted to the aspnet
user. After giving "Everyone" full control of this file, the code executed
without error. Do you have the file open in an XML editor also? Maybe it
really is being used by another process?
 
W

Web Team @ Borough of Poole

Hi Andy,

I followed your plan and produced a standalone page looking at a
standalone XML file (Not part of the site) and the code works as
expected everytime.

Looks like I need to track down where I am leaving the XML file open.

Thank you very much for your help!

Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top