473,811 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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><rec ord/><record/></root>";

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

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

Mar 10 '06 #1
2 1523


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><rec ord/><record/></root>";

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


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

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

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

xmlWriter.Write StartElement("d oc");
xmlWriter.Write AttributeString ("myXml", myXml);
xmlWriter.Write EndElement();
xmlWriter.Flush ();
xmlWriter.Close ();

string xml = stringWriter.To String();

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

Result then is e.g.

<doc myXml="&lt;root &gt;&lt;reco rd/&gt;&lt;reco rd/&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*******@yaho o.de> wrote in message
news:eV******** ******@TK2MSFTN GP14.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><rec ord/><record/></root>";

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


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

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

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

xmlWriter.Write StartElement("d oc");
xmlWriter.Write AttributeString ("myXml", myXml);
xmlWriter.Write EndElement();
xmlWriter.Flush ();
xmlWriter.Close ();

string xml = stringWriter.To String();

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

Result then is e.g.

<doc myXml="&lt;root &gt;&lt;reco rd/&gt;&lt;reco rd/&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
1881
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 it. I have tried a couple of methods as follows: <head> <meta http-equiv="refresh"
1
2182
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 work. It works if the tags are <input type=radio...>. This line is OK:
6
10164
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 have the function update the target ID with the URL. There is a bit more to it then that, but that is the basics. my difficulty comes when I try to assign a variable to: "document.getElementById('-> var gose here <-').innerHTML"
1
1704
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. Private Sub btnTesting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTesting.Click Dim cn As New SqlClient.SqlConnection Dim cm As New SqlClient.SqlCommand
8
1924
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 background info: I'm an amateur who makes sites for fun. I'm creating a website for my son's small school. It's a table-less design. I'm basically using a Container DIV with a Content DIV and a right-floating Sidebar DIV.
20
7020
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
15
2342
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 myhtml page in series manner like oragnisation0,oragnisation1,oragnisation2 and so on, and popup window will open onChange event of (oraganisation<iCount>) with one query string / parameter i.e value of oraganisation so that i will use that...
1
1770
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"> <h2>Easy MLS Search for Oregon and Southwest Washington:</h2> <div class="content"> </div> </div>
1
1533
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 available (i.e. using an adapter, or something like the strategy pattern). To give an example and a non-example of what I'm talking about, consider the following recipes from the online Python Cookbook: Ring Buffer:...
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10648
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10135
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.