XmlTextWriter

I am using the XmlTextWriter class to generate some XML however if I
try to do something like

string str = "cost �500";
xtw.WriteElementString("symbol", str);

The � does not get encoded to £ like it should. I have tried
changing the encoding of it but none of the options (Unicode, UTF8
etc.) perform the encoding correctly.

At the moment I have just replaced the above with 

xtw.WriteStartElement("string");
str = "cost �500";
str = str.Replace("�", "£");
xtw.WriteRaw(str);
xtw.WriteEndElement();

Which does the trick but any other characters that need encoding will
have to be added to the str = str.Replace("�", "£"); line. Is
there anything to perform this encoding automatically?

Thanks in advance,



Rob.
0
weeerob (2)
9/15/2003 2:13:17 PM
dotnet.xml 7266 articles. 0 followers. Follow

4 Replies
507 Views

Similar Articles

[PageSpeed] 10

Robert Walter wrote:

> I am using the XmlTextWriter class to generate some XML however if I
> try to do something like
>=20
> string str =3D "cost =A3500";
> xtw.WriteElementString("symbol", str);
>=20
> The =A3 does not get encoded to £ like it should.
Why do you think it should do that? What's so special in =A3 character?
--=20
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

0
oleg7603 (294)
9/15/2003 3:37:52 PM
Oleg Tkachenko <oleg@NO SPAM PLEASEtkachenko.com> wrote in message news:<OqGQTZ5eDHA.2356@TK2MSFTNGP12.phx.gbl>...
> Robert Walter wrote:
> 
> > I am using the XmlTextWriter class to generate some XML however if I
> > try to do something like
> > 
> > string str = "cost  500";
> > xtw.WriteElementString("symbol", str);
> > 
> > The   does not get encoded to &#163; like it should.
> Why do you think it should do that? What's so special in   character?

It should do that 'cause if you put a pound symbol in an XML document
it's not valid.
0
weeerob (2)
9/16/2003 1:49:28 PM
Robert Walter wrote:

> It should do that 'cause if you put a pound symbol in an XML document
> it's not valid.
Not really. Pound symbol is allowed in XML documents as thousands of other 
unicode symbols, try yourself: http://www.tkachenko.com/temp/source.xml

The only two symbols must be escaped in XML: & and <.
-- 
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

0
oleg7603 (294)
9/16/2003 3:13:06 PM
Hello!

If you specify the correct encoding in the XML document declaration, it'll
display itself as a valid document. Did you try that?

-- 
venlig hilsen / with regards
anders borum
--


0
na1006 (31)
9/16/2003 5:04:40 PM
Reply:

Similar Artilces:

XmlTextWriter and whitespace
Hi, I've a routine that exports a DB query to Excel by building an XmlDocument and saving to a XmlTextWriter. The function works but new lines don't show up, i.e. significant whitespace is cut. I've tried adding WriteAttributeString("xml", "space", null, "preserve") to the XmlTextWriter just prior to save, but that results in the error: "Index was outside the bounds of the array." Can anyone assist? Many thanks, Simon Lane Have you tried playing with the PreserveWhitespace property of XmlDocument? <st@jpa.co.jp> wrote in message...

how do I import a XMLTextWriter into an XMLDOCUMENT?
Hi: I want to append a new transaction to a transaction log. The transaction log is trans.xml <transactions> <transaction> <id>12</id> <stamp>1/1/1900 12:02</stamp> </transaction> </transactions> Basically I have been able to make the new TRANSACTION fragments using an XMLTextWriter. My XMLDocument loads the xml file from disk. How do I 'import' or 'insert' the contents of the XMLTextWriter into my XMLDocument. All of the examples I have seen write straight to the file, but replace whatever was al...

XmlTextWriter
I am using the XmlTextWriter class to generate some XML however if I try to do something like string str = "cost �500"; xtw.WriteElementString("symbol", str); The � does not get encoded to &#163; like it should. I have tried changing the encoding of it but none of the options (Unicode, UTF8 etc.) perform the encoding correctly. At the moment I have just replaced the above with xtw.WriteStartElement("string"); str = "cost �500"; str = str.Replace("�", "&#163;"); xtw.WriteRaw(str); xtw.WriteEndElement(); Which does the tri...

XmlTextWriter.WriteString
Hi, I've been working on a bug in some production software using XmlTextWriter. Basically, I need to but a Surname in an element, the surname in question is O'Connor. Because of the ' I've used the following code: .WriteStartElement("Surname") .WriteString(client.Surname) .WriteEndElement() assuming that the ' would be encoded to &apos;. It isn't!! Am I missing something here? Regards * Gavin Sullivan wrote in microsoft.public.dotnet.xml: >I've been working on a bug in some production software using XmlT...

XMLTextWriter and SOAP
Hi guys, I have a task to do where I need to write an XML file into SOAP format. I use XMLTextWriter in order to write my strings to the XML file, but I must convert somehow this xml into SOAP. How can I do that ? Is there a way to do it without hardcoding SOAP attributes and elements ? As exemple, a function that would receive a stream and convert the stream to SOAP...I don't really know where to look so this is why I post here :) Thanks guys, Guillaume Sorry, I forgot to mention that I'm using VB.Net and must not serialize by using SoapFormatter object. ...

looping Datarows with XMLTEXTWRITER
Hi, I am trying to create an XML file from a SQL view that I have in my database. The code woeks fine but I have a problem trying to define an atribute. For example PO number 123 contains 2 lines I wish to write it to the XML file as <PONUM>1 <POLINE>1 <ITEM>Apple</ITEM> </POLINE> <POLINE>2 <ITEM>PEAR</ITEM> </POLINE> </PONUM> I get this at the moment because I can not loop the PO line part <PONUM>1 <POLINE>1 <ITEM>Apple&l...

XmlTextWriter,XmlDocument
Hi all, I need to embed an image to an existing xml file. I guess this is possible using XmlTextWriter.WriteBase64() method. My problem is that XmlTextWriter writes to a new document. How can I edit an existing doc (for example with XmlDocument class) and write an image to it? I'm sorry but I'm a bit confused about the difference between XmlTextWriter and XmlTextDocument. Thanks. Ars Comm - Ciro Ferraiuolo wrote: > Hi all, I need to embed an image to an existing xml file. I guess this is > possible using XmlTextWriter.WriteBase64() method. My problem is that > XmlTextWrit...

determining current element in an XmlTextWriter
Is there a way to determine what the current open XML element is in an XmlTextWriter, without reading back the stream? Thanks. Jon I don't think so. Instead save name of an element being written into a local variable. >-----Original Message----- >Is there a way to determine what the current open XML element is in an >XmlTextWriter, without reading back the stream? ...

XmlTextWriter
Hello: I would like to output the following XML Element using the XmlTextWriter, but I cannot get it correct: ----------------------------------------------------------------------------------------------- <SOAP-ENV:Body xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" SOAP-SEC:id="body"> ----------------------------------------------------------------------------------------------- I have tried the following: xw.WriteStartElement("SOAP-ENV", "Body", "123"); xw.WriteAttributeString("xmlns", "SOAP-SEC&...

xmlTextWriter #2
Hello, How do I use this class to create an XML document without saving it into a file? I need to store the XML documen into a string so it could be passed as a parameter to another class. Gilgamesh Create an XmlTextWriter through the constructor which takes in a TextWriter. You could create a StringWriter instance of the abstract TextWriter class and store the content written by the XmlTextWriter is that string without ever persisting it into a file. Thanks, Zafar "Gilgamesh" <gilgamesh4ever@aol.com> wrote in message news:%23B$UHWRVGHA.4864@TK2MSFTNGP12.phx.gbl... >...

newlines with XmlTextWriter
Hi, I would like to use an XmlTextWriter to format the XML so that each element (or end element) appears on a new line, regardless of newlines in the original document. The effect I'm looking for is something similar to XMLSpy's pretty print. I realise that, since whitespace is signiifcant, I am not going to get out exactly the correct DOM document output, but I am formatting for readability not automatic processing. I can't see anything in XmlTextWriter or XmlWriterSettings to enable this. is it possible? TIA Andy Andy Fish wrote: > I would like to use an XmlText...

XMLTextWriter and XMLTextReader
I am looking for a way to open an xmltextwriter to append to an existing document. Currently I rename the existing document, then I created new one and copy over all the existing documents nodes. I would rather avoid renaming the files. These files can become quite large so I need to avoid loading an xmldocument as well. Does anyone have a pointer for me to review? Thanks in advance. I think I am going to go with this concept http://www.tkachenko.com/blog/archives/000053.html, I think it will suit my needs just fine. "amdrit" <amdrit@hotmail.com> wrote in messa...

XmlTextWriter and QuoteChar ???
Hi, I'm trying to specify single-quotes to be used in xmlAttributes as follows : XmlTextWriter writer = new XmlTextWriter (filename, null); writer.QuoteChar = (char)39; --> but he still generates double quotes ??? thnx for your help Chris Chris, I found this in a book and it works great for double quotes: xtw.QuoteChar = """"c you could try this for single quotes: xtw.QuoteChar = "''"c HTH... Scott "Chris" wrote: > Hi, > > I'm trying to specify single-quotes to be used in xmlAttributes as follows : > ...

XmlTextWriter: Why is endElement not written using the WriteEndElement with WriteRaw
Can somebody explain why the endElement (</items>) in the following sample is not written? Public Sub Test() Dim objMem As New MemoryStream Dim objWriter As New XmlTextWriter(objMem, New System.Text.ASCIIEncoding) Dim strXMLResult As String objWriter.WriteStartDocument() objWriter.WriteStartElement("items") objWriter.WriteRaw(GetItem(1)) objWriter.WriteEndElement() 'Why is this endElement not writen? objWriter.WriteEndDocument() objWriter.Flush() objWriter.Close() strXMLResult = S...

getting the xml from XmlTextWriter
We have an app that uses the XmlTextWriter to write XML to the local disk. It works and it's great. Now they want to write it to the database instead. Is there a way that I can just get the XML out of the XmlTextWriter? I looked at the XmlTextWriter class and can't seem to figure out how to take the XML out of it. I want to do this dim x as string x = XmlTextWriter.XML Thanks in advance, Rob * SneakyMonki wrote in microsoft.public.dotnet.xml: >We have an app that uses the XmlTextWriter to write XML to the local disk. It >works and it's great. Now they want to write i...

XmlTextReader and XmlTextWriter
Hi - I am trying to read an XML file using XmlTextReader and write it out using XmlTextWriter. The XML is XHTML. At the moment, I am trying to create a straightforward copy of a file. The XHTML file contains nodes and attributes (with there own namespace) that will later be used to alter the XML into a desired output. This 'copying' is the first stage to then processing the XML to the desired output I have included the code below. If I put debug stops on in the appropriate place, the reader is correctly and moving through the XML. However, the XML writing is incorrect. It seem...

using XmlTextWriter
if there are no data when writing an element is creates the tag like this <customer/> but i would like it to write <customer> </customer> any property i can set or something else i could do ?? gusse.net wrote: > if there are no data when writing an element is creates the tag like this > > <customer/> > > but i would like it to write > > <customer> > </customer> > > any property i can set or something else i could do ?? <customer/> is an emtpy element while <customer> </customer> is not an ...

Problem with XmlTextWriter
Hi, I am currently trying to output my environment variables to a file in XML. It's currently failing with an InvalidOperationException when it executes the xw.WriteStartElement("Variable") for the second time. The exception details are: "Token StartElement in state Epilog would result in an invalid XML document." I'm sure it's something obvious. What am I missing? Regards, Tony XmlTextWriter xw = new XmlTextWriter(@"C:\test.xml",Encoding.UTF8); xw.WriteStartDocument(); foreach (DictionaryEntry de in Environment....

XmlTextWriter vs XPathNavigator vs XmlDocument
Hey Folks. If I want to create a simple XMLDocument that contains a few lines of XML which class should I use? I'm a bit confused of the differences and which class to use and when. Ta. * Mr Flibble wrote: > Hey Folks. > > If I want to create a simple XMLDocument that contains a few lines of > XML which class should I use? I'm a bit confused of the differences and > which class to use and when. > > Ta. Oops I forgot to say that I want to create this XML in memory since it will be POST'd to a website.. Mr Flibble wrote: >>If I want to create ...

How to create this with XmlTextWriter
Is this even possible using XmlTextWriter? Or would I need to use an XmlDocument? I need to create the following XML: <?xml version="1.0"?> <AccessRequest xml:lang="en-US"> <AccessLicenseNumber>YOURACCESSLICENSENUMBER</AccessLicenseNumber> <UserId>YOURUSERID</UserId> <Password>YOURPASSWORD</Password> </AccessRequest> When I use the XmlTextWriter, I always get this as the first line: <?xml version="1.0" encoding="utf-16"?> I also need help adding the: xml:lang="en-US" to the...

how do i use xmltextwriter
I would like to build an XML document in memory (to save disk IO) to submit to another process. Rather than write to a file, I'm trying to using xmltextwriter with a memory stream but the stream never seems to get populated. Has anyone done this successfully? David wrote: > I would like to build an XML document in memory (to save > disk IO) to submit to another process. Rather than write > to a file, I'm trying to using xmltextwriter with a memory > stream but the stream never seems to get populated. > > Has anyone done this successfully? Have you Flush()...

XmlReader to XmlTextWriter without XmlDocument
Im currently using the following code.. XmlDocument xmlData = new XmlDocument(); XmlTextWriter xmlwriter = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8); XmlReader xmlreader = SqlHelper.ExecuteXmlReader(...) xmlData.Load(xmlreader); xmlData.WriteTo(xmlwriter); xmlwriter.Flush(); Is there a way to get around using the XmlDocument for this situation? Thanks in advance! Hi, I think you could move to the root node by calling XmlReader.MoveToContent, then XmlReader.ReadOuterXml and write the returned string to the response. That only write the root node content, so if yo...

XmlTextWriter without a file
can anyone help me on how to create and manipulate a xmttextwriter without having to craete a physical file. I have an application that should return data in xml. But I do not want to create a file and then delete it. /Magnus Magnus wrote: > can anyone help me on how to create and manipulate a xmttextwriter without > having to craete a physical file. > > I have an application that should return data in xml. But I do not want to > create a file and then delete it. Where does the application need to return the data to? If it needs to return the data to a stream or ...

HELP! XSLTranform and XMLTextWriter Error
I have tried everything with this! I get an error stating "Index was outside the bounds of the array" My code looks like this.... xmlDoc = New XmlDocument() xmlDoc.Load("xml.doc") xslDoc = New XslTransform() xslDoc.Load("xsl.doc") Dim TWrtr As New XmlTextWriter("c:\somelocation.doc", System.Text.Encoding.UTF8) xslDoc.Transform(xmlDoc, Nothing, TWrtr) TWrtr.Close() xmlDoc = Nothing xslDoc = Nothing i do not want to post the XSL/XML as it has customer data in it and it is extrem...

XmlTextWriter Usage question
I would like to store the contents of the xml created via the XmlTextWriter to a string as opposed to a file. Is this possible ? If so how ? Is this the best way to create xml that gets stored to a variable? Am I better off using the xmldom ? This is possible, in fact it's even pretty easy. Create a StringWriter that writes to a stringbuilder, or a stream based on a memory stream. Pass either of these to the XmlTextWriter, perform your operations, then pull your string out of the stringbuilder or the memory stream. It's about 3 lines of code, total... -- Chris Mullins,...