Problems with loading xml from XmlTextWriter

I use an XmlTextWriter to generate xml data.

The declaration is:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
XmlTextWriter w = new XmlTextWriter(sw);

And I write to it using

....
w.WriteElementString("foo","bar");

When the creation is done, I need to load it into DOM to select an 
XmlNode or an XmlElement to apply to another method (which I cannot 
control).

I thought I could do it with:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(sb.ToString());
XmlNode node = xDoc.SelectSingleNode("sub-header");
XmlNode axaptaData = SomeManager.TransferDataToAxapta(node);

But at run-time I get an error saying:

"the path is too long after being fully qualified"

(when the .Load()-method is executed)

How do I get arround this?

.... and also:

The XmlTextWriter inserts the XmlDeclaration as

<?xml version="1.0" Encoding="utf-16" ?>

Is there any wy to make it write it as utf-8? I cannot apply the Encoding 
as the constructor of the class, since it will then not accept my 
StringWriter as first parameter.

Can you help me out with this one?

Thanks,

:o)

-- 
Jesper Stocholm http://stocholm.dk

Programmer's code comment:
//It probably makes more sense when you're stoned.
0
j3904 (5)
3/5/2004 5:02:58 PM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
612 Views

Similar Articles

[PageSpeed] 52

Jesper Stocholm wrote:


> xDoc.Load(sb.ToString());

Load() method accepts URL of a document to load. Use LoadXml() method to 
load XML from string.

-- 
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
0
Oleg
3/5/2004 5:40:41 PM
Oleg Tkachenko [MVP] wrote :

> Jesper Stocholm wrote:
>
>> xDoc.Load(sb.ToString());
> 
> Load() method accepts URL of a document to load. Use LoadXml() method
> to load XML from string.

Geeees, how could I miss this?

:o)

An additional question:

The Xml is in the structure:

<?xml version="1.0" ?>
<sub-header>
    	<header>
    	    	<field1/>
    	    	<field2/>
    	    	<line>
    	    	    	<fieldx/>
    	    	    	<fieldy/>
    	    	</line>
    	</header>
</sub-header>

I need to extract the entire <sub-header/>-section to an XmlNode and
pass it to another function. 

I thought about doing it like this:

xDoc.LoadXml(<my data>);
XmlNode node = xDoc.SelectSingleNode("sub-header");
SomeMethod(node);

But is there any easier way to do it? Memory-usage for the service I am
writing is essential (to limit), so if there is a more efficient way to
do it, I would prefer this. 

:o)

-- 
Jesper Stocholm
http://stocholm.dk
Linux advocacy how-to: 
http://www.datasync.com/~rogerspl/Advocacy-HOWTO-5.html
0
j3904 (5)
3/5/2004 7:35:23 PM
Reply:

Similar Artilces: