Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 10th, 2006, 03:05 PM
McGeeky
Guest
 
Posts: n/a
Default 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



  #2  
Old March 10th, 2006, 03:26 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Assigning XML string as the content of an attribute



McGeeky wrote:

[color=blue]
> 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 ) + "'/>";[/color]

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/
  #3  
Old March 10th, 2006, 03:35 PM
McGeeky
Guest
 
Posts: n/a
Default Re: Assigning XML string as the content of an attribute

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" <mahotrash@yahoo.de> wrote in message
news:eVfL6UFRGHA.6084@TK2MSFTNGP14.phx.gbl...[color=blue]
>
>
> McGeeky wrote:
>
>[color=green]
>> 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 ) + "'/>";[/color]
>
> 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/[/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles