xmltextwriter escaping characters

  • Thread starter Thread starter FabSW
  • Start date Start date
F

FabSW

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"
 
FabSW said:
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)

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:

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"

How exactly do you populate the XmlDocument instance? Can you show us
that code?
 
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();
}
}
 
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();
}
}

double quote into sOggetto is char code 147
 
Back
Top