c# 4.0 - xml error: Non white space characters cannot be added to content -


i trying open xmldocument this:

var doc = new xdocument("c:\\temp\\contacts.xml"); var reader = doc.createreader(); var namespacemanager = new xmlnamespacemanager(reader.nametable); namespacemanager.addnamespace("g", g.namespacename); var node = doc.xpathselectelement("/contacts/contact/g:name[text()='patrick hines']", namespacemanager); node.value = "new name richard"; doc.save("c:\\temp\\newcontacts.xml"); 

i returns error in first line:

non whitespace characters cannot added content. 

the xmlfile looks this:

<?xml version="1.0" encoding="utf-8"?> <contacts xmlns:g="http://something.com">   <contact>     <g:name>patrick hines</g:name>     <phone>206-555-0144</phone>     <address>       <street>this street</street>     </address>   </contact> </contacts> 

it looks you're attempting load xml file xdocument, need call xdocument.load("c:\\temp\\contacts.xml"); - can't pass xml file constructor.

you can load string of xml xdocument.parse(stringxml);.

change first line to:

var doc = xdocument.load("c:\\temp\\contacts.xml"); 

and work.

for reference, there 4 overloads of xdocument constructor:

xdocument(); xdocument(object[]); xdocument(xdocument); xdocument(xdeclaration, object[]); 

you might have been thinking of third 1 (xdocument(xdocument)), use 1 you'd have write:

var doc = new xdocument(xdocument.load("c:\\temp\\contacts.xml")); 

which redundant when var doc = xdocument.load("c:\\temp\\contacts.xml"); suffice.

see xdocument constructor gritty details.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -