> That's a code from 1998. In 2005 you can have a luxury to use
XmlTextWriter.
Yes, I get your point :-)
But I have 2 reasons to believe in my very simplistic solution:
- The example Scott provides is a serie of commands. They don't seem to have
any special chars. Special chars like "&", ">" and "<". In his particular
case, he may have no escapes to deal with.
- The resulting code is very "readable".
To deal with escape chars, you can use the following function:
StringBuilder xml=new StringBuilder();
xml.Append("<CommandXML>\n");
xml.Append(" <cmd name=\"1\" action=\"2\">\n");
xml.Append(" <arg name=\"3\"
value=\""+ParseForXml("&<>&&&")+"\"/>\n");
xml.Append(" <arg name=\"5\" value=\"6\"/>\n");
xml.Append(" </cmd>\n");
xml.Append(" <cmd name=\"7\" action=\"9\">\n");
xml.Append(" <arg name=\"9\" value=\"0\"/>\n");
xml.Append(" </cmd>\n");
xml.Append("</CommandXML>");
Then you have to define the ParseForXml method somewhere:
public static string ParseForXML(string str) {
string s;
int n1,n2,n3;
StringBuilder xml;
if (str==null) return "";
if (str.Length==0) return "";
xml=new StringBuilder("");
//.......................... Remplace les "&"
.............................................
// Les "&" sont légaux sous la forme "&", "<" et ">"
// Les "&" sont aussi légaux sous la forme "&#--" et "&#x----"
n3=0;
n1=str.IndexOf("&");
while (n1>=0) {
xml.Append(str.Substring(n3,n1-n3));
n3=n1+1;
n2=str.IndexOf(";",n1);
if (n2>n1) {
s=str.Substring(n1,n2-n1+1);
switch (s) {
case "&":
case ">":
case "<":
xml.Append("&");
break;
default:
if (s[1]=='#')
xml.Append("&");
else
xml.Append("&");
break;
}
} else
xml.Append("&");
n1=str.IndexOf("&",n3);
}
xml.Append(str.Substring(n3));
//................. Remplace les ">" et les "<"
..................................
xml.Replace("<","<");
xml.Replace(">",">");
//................................ Et voilà
......................................
return xml.ToString();
}
Of course, if the Xml data you have to write has external namespaces, really
complex Unicode chars, etc, it may get tricky. And, truely, the
XmlTextWriter may be a nice solution:
xml=new StringBuilder();
sw=new StringWriter(xml);
xtw=new XmlTextWriter(sw);
xtw.WriteStartDocument(true);
xtw.WriteStartElement("CommandXML");
xtw.WriteStartElement("cmd");
xtw.WriteAttributeString("name","1");
xtw.WriteAttributeString("action","2");
xtw.WriteStartElement("arg");
xtw.WriteAttributeString("name","3");
xtw.WriteAttributeString("value","4");
xtw.WriteEndElement();
xtw.WriteStartElement("arg");
xtw.WriteAttributeString("name","5");
xtw.WriteAttributeString("value","6");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteStartElement("cmd");
xtw.WriteAttributeString("name","7");
xtw.WriteAttributeString("action","9");
xtw.WriteStartElement("arg");
xtw.WriteAttributeString("name","9");
xtw.WriteAttributeString("value","0");
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
XmlDocument doc=new XmlDocument();
doc.LoadXml(xml.ToString());
But I think is poorly readable. And I still prefere the simple way. After
all Scott was asking for the "simplest".
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:O%******************@TK2MSFTNGP09.phx.gbl...
jmgonet wrote:
Well, may be I'm simplistic, but if you just want to send a string to a
server, then build the string:
Bad idea. Then you must take care of XML syntax issues - well-formdness,
escaping special characters, encoding issues etc etc etc. It's always
much better to let XML API to deal with XML.
StringBuilder xml=new StringBuilder();
xml.Append("<CommandXML>\n");
That's a code from 1998. In 2005 you can have a luxury to use
XmlTextWriter.
--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com