Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

xmltextwriter escaping characters

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FabSW - 27 Sep 2007 16:35 GMT
hi all, i've to save to xml some data stored in sqlserver database,
i've to use iso-8859-1 encoding.

if i write text into xml attributes,
there is some characters that make invalid xml (for example " (147))

xmltextwriter replace character (147)  with doublequote (34)

when i write this text into xml attrribute the double quote into text
is interpreted by writer like an xml attribute
delimiter.

this is my code:

using (FileStream fs = new FileStream(sOutputFile,
FileMode.CreateNew, FileAccess.Write))
               {
                   using (XmlTextWriter writer = new
XmlTextWriter(fs, Encoding.GetEncoding(_XmlEncoding)))
                   {
                       XmlDoc.Save(writer);
                       writer.Flush();
                       writer.Close();
                   }
                   fs.Close();
               }

XmlDoc is XMLDocument
_XmlEncoding is string ="ISO-8859-1"
Martin Honnen - 27 Sep 2007 16:52 GMT
> hi all, i've to save to xml some data stored in sqlserver database,
> i've to use iso-8859-1 encoding.
[quoted text clipped - 3 lines]
>
> xmltextwriter replace character (147)  with doublequote (34)

ISO-8859-1 is shown here:
<http://www.microsoft.com/globaldev/reference/iso/28591.mspx>. 147 is
not assigned any character.

> this is my code:
>
[quoted text clipped - 13 lines]
> XmlDoc is XMLDocument
> _XmlEncoding is string ="ISO-8859-1"

How exactly do you populate the XmlDocument instance? Can you show us
that code?

Signature

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

FabSW - 27 Sep 2007 17:47 GMT
> > XmlDoc is XMLDocument
> > _XmlEncoding is string ="ISO-8859-1"
>
> How exactly do you populate the XmlDocument instance? Can you show us
> that code?

HI thanks for reply, this is the code:

      using System.Xml;
      using System.IO;

       private void button1_Click(object sender, EventArgs e)
       {
           CreateXml();
       }

       private void CreateXml()
       {

           XmlDocument xml = new XmlDocument();
           XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0",
"ISO-8859-1", null);
           XmlElement root = xml.DocumentElement;
           xml.InsertBefore(xmldecl, root);

           //IdProdotto = (int)drProdotti["IdProdotto"];
           int IdProdotto = 1;

           AppendNode(xml, "<DocEnvelope />", "/", string.Empty);
           this.AppendNode(xml, "<Doc />", "/DocEnvelope",
string.Empty);

           XmlNode prodotto = AppendNode(xml, "<Prodotto />", "/
DocEnvelope/Doc", string.Empty);

           this.AddAttribute(xml, prodotto, "IdProdotto",
IdProdotto.ToString());

           string sOggetto = ""text"";
           //this.AddAttribute(docEnvelope.XmlDoc, prodotto,
"Oggetto", drProdotti["Oggetto"].ToString());
           this.AddAttribute(xml, prodotto, "Oggetto", sOggetto);

           SaveToXml(@"c:\output.xml", xml);
       }

       public XmlNode AppendNode(XmlDocument xml, string xmlFragment,
string xPath, string defaultNode)
       {
           XmlTextReader reader = null;
           XmlNode xmlNode = null;
           try
           {
               if (xmlFragment == null && defaultNode != null)
               {
                   xmlFragment = defaultNode;
               }

               if (xmlFragment != null)
               {
                   reader = new XmlTextReader(xmlFragment,
XmlNodeType.Element, null);
                   xmlNode = xml.SelectSingleNode(xPath).AppendChild(
                       xml.ReadNode(reader));
               }
           }
           catch
           {
               return null;
               //throw ex;
           }
           finally
           {
               if (reader != null)
               {
                   reader.Close();
               }
           }
           return xmlNode;
       }

       void AddAttribute(XmlDocument XmlDoc, XmlNode node, string
Name, string Value)
       {
           XmlAttribute att = XmlDoc.CreateAttribute(Name);
           att.InnerText = Value;
           node.Attributes.SetNamedItem(att);
       }

       public void SaveToXml(string sFileName, XmlDocument xml)
       {

           using (FileStream fs = new FileStream(sFileName,
FileMode.CreateNew, FileAccess.Write))
           {
               using (XmlTextWriter writer = new XmlTextWriter(fs,
Encoding.GetEncoding("ISO-8859-1")))
               {
                   writer.Formatting = Formatting.Indented;
                   xml.Save(writer);
                   writer.Flush();
                   writer.Close();
               }
               fs.Close();
           }
       }
FabSW - 27 Sep 2007 17:51 GMT
> > > XmlDoc is XMLDocument
> > > _XmlEncoding is string ="ISO-8859-1"
[quoted text clipped - 102 lines]
>             }
>         }

double quote into sOggetto is char code 147

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.