473,320 Members | 1,868 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,320 software developers and data experts.

Assigning XML string as the content of an attribute

Hi. I want to assign an XML string to an XML attribute. This XML string must
undergo "escape" conversion so that the < and & symbols are converted in to
escaped equivalents.

Does the .Net library have a conversion method that does this? Note that I
want to construct the resulting XML string myself without having to use an
XmlDocument.

E.g.

string myXml = "<root><record/><record/></root>";

string myDoc = "<doc myXml='" + Converter.escapeMyXml (myXml ) + "'/>";

--
McGeeky
http://mcgeeky.blogspot.com

Mar 10 '06 #1
2 1503


McGeeky wrote:

Does the .Net library have a conversion method that does this? Note that I
want to construct the resulting XML string myself without having to use an
XmlDocument.

E.g.

string myXml = "<root><record/><record/></root>";

string myDoc = "<doc myXml='" + Converter.escapeMyXml (myXml ) + "'/>";


Sure, you should always use XmlTextWriter if you want to construct/write
some XML, here is a simple example:

string myXml = "<root><record/><record/></root>";

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

xmlWriter.WriteStartElement("doc");
xmlWriter.WriteAttributeString("myXml", myXml);
xmlWriter.WriteEndElement();
xmlWriter.Flush();
xmlWriter.Close();

string xml = stringWriter.ToString();

Console.WriteLine("XML created is:\r\n{0}", xml);

Result then is e.g.

<doc myXml="&lt;root&gt;&lt;record/&gt;&lt;record/&gt;&lt;/root&gt;" />
So .NET provides all what you need. Only I have doubts that putting XML
escaped into an attribute is usually a good idea as that way it loses
its structure and is plain text for any XML tool.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 10 '06 #2
Thanks for that! Its really helped.

The reason for the XML in the attribute is that I am passing it as a
parameter to a stored procedure which then uses openxml to read from it.

--
McGeeky
http://mcgeeky.blogspot.com
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:eV**************@TK2MSFTNGP14.phx.gbl...


McGeeky wrote:

Does the .Net library have a conversion method that does this? Note that
I want to construct the resulting XML string myself without having to use
an XmlDocument.

E.g.

string myXml = "<root><record/><record/></root>";

string myDoc = "<doc myXml='" + Converter.escapeMyXml (myXml ) + "'/>";


Sure, you should always use XmlTextWriter if you want to construct/write
some XML, here is a simple example:

string myXml = "<root><record/><record/></root>";

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

xmlWriter.WriteStartElement("doc");
xmlWriter.WriteAttributeString("myXml", myXml);
xmlWriter.WriteEndElement();
xmlWriter.Flush();
xmlWriter.Close();

string xml = stringWriter.ToString();

Console.WriteLine("XML created is:\r\n{0}", xml);

Result then is e.g.

<doc myXml="&lt;root&gt;&lt;record/&gt;&lt;record/&gt;&lt;/root&gt;" />
So .NET provides all what you need. Only I have doubts that putting XML
escaped into an attribute is usually a good idea as that way it loses its
structure and is plain text for any XML tool.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 10 '06 #3

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

Similar topics

1
by: Neil Thompson | last post by:
Hi I am trying to write out some a string constant that needs to include a couple of XML elements. The parser is, quite rightly, complaining about what I have done but I don't know how to solve...
1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
1
by: mhnazly | last post by:
i'm trying to read data from SQL Server database using data reader and assigned it to a label in my asp.net web application. but when the button is clicked, nothing appears. please help, thanks. ...
8
by: Viken Karaguesian | last post by:
Hello all, I'll start with this question: Can I assign an ID *and* a CLASS to a DIV? I am under the impression that you can. I'm having a problem that I can't seem to figure out. Some...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
15
by: Kapil Jain | last post by:
Dear All, What i need to achieve is : I am generating dynamic text boxes thru dhtml coding, i need onChange event of oragnistation text box i.e dynamically generated on click of "More" button in...
1
by: speralta | last post by:
For some reason the text in h2 tag is displaying as white in IE. http://www.salperalta.com/ <td class="sidebar" id="sidebar-right"> <div class="block block-listing" id="block-listing-0">...
1
by: The Pythonista | last post by:
I've been wondering for a while about whether assigning to __class__ is bad form or not. Specifically, I mean doing so when some other method of implementing the functionality you're after is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.