473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Updating contents of XML node

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(s trXMLFileLocation))
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea" &
textAreaID)
loNode.InnerText = strContent
loXMLDoc.Save(HttpContext.Current.Server.MapPath(s trXMLFileLocation))
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(s trXMLFileLocation))
loXMLDoc.Load(objStreamReader)
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea" &
textAreaID)
loNode.InnerText = strContent
objStreamReader.Close()
objStreamReader = Nothing
loXMLDoc.Save(HttpContext.Current.Server.MapPath(s trXMLFileLocation))
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.

Nov 19 '05 #1
4 1433
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.

"Web Team @ Borough of Poole" wrote:
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(s trXMLFileLocation))
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea" &
textAreaID)
loNode.InnerText = strContent
loXMLDoc.Save(HttpContext.Current.Server.MapPath(s trXMLFileLocation))
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(s trXMLFileLocation))
loXMLDoc.Load(objStreamReader)
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea" &
textAreaID)
loNode.InnerText = strContent
objStreamReader.Close()
objStreamReader = Nothing
loXMLDoc.Save(HttpContext.Current.Server.MapPath(s trXMLFileLocation))
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.

Nov 19 '05 #2
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(s trXMLFileLocation))
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea"
& textAreaID)
loNode.InnerText = strContent

loXMLDoc.Save(HttpContext.Current.Server.MapPath(s trXMLFileLocation))
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.

Nov 19 '05 #3
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?
"Web Team @ Borough of Poole" wrote:
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(s trXMLFileLocation))
loNode = loXMLDoc.SelectSingleNode("/Site/Content/TextArea"
& textAreaID)
loNode.InnerText = strContent

loXMLDoc.Save(HttpContext.Current.Server.MapPath(s trXMLFileLocation))
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.

Nov 19 '05 #4
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.

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rostov | last post by:
I've got a script where I'm trying to toggle the visibility of a div node by a click on an image that is the sibling of the div. So I've got this HTML: <div> <img...
3
by: J Krugman | last post by:
This question refers to the DOM. I would like to dynamically append some small amount of text to the all-text content of a <pre> node. I suppose that I could extract the existing content, and...
4
by: JoBean | last post by:
I think this is a simple Q but I cannot get the syntax right. I want to specify the node name and get its contents. I do not want to loop through and grab the contents of all child nodes - which...
9
by: Rocky | last post by:
I have 2 textboxes. When I click submit, i want to add whatevers in the text box1 as username and whatevers in textbox2 as userid into an xml file. How do I do that in ASP.NET using vb.net? My...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
0
by: drop | last post by:
Hi, I'm currently working with the Treeview control in ASP .Net 2.0. The tree is filled dynamically based on data contained in a MySQL Database. Here is the exact behavior I want : 1 - User...
2
by: Pugi! | last post by:
Using AJAX I want to send some information from the server (php-page) as XML to the client. The contents can be very divers so I have to use XML instead of text. On one occasion the contents is...
1
by: Tedros.G | last post by:
Hi I have an attribute the appears in both the root node and child node for example, below the attribute VERSION appears in the rood node (PRODMSG ) and a child node (OPERATION ) ================...
0
by: Falcula | last post by:
Hello, I have a treeview that i fill from a database, when i update nodename in database the treeview dont update. Its works when iam not useing enableviewstate="true" but then i loosing the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.