471,618 Members | 1,315 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

Net 2.0 XmlWriter Create and Attributes

The following two cases behave differently in Net 2.0. The
Case_Create_StringWriter throws an exception while the Case_XmlTextWriter
does not.

Is there a problem with this code?
Or is this a known problem?

--------------------------------------------------------------------------
using System;
using System.Xml;
using System.Text;

namespace TestXmlNil2005
{
class Program
{
static void Main(string[] args)
{
Case_XmlTextWriter();
Case_Create_StringWriter();
}

static void Case_Create_StringWriter()
{
try
{
System.IO.StringWriter sw = new System.IO.StringWriter();

XmlWriter xw = XmlWriter.Create(sw);

WriteXml(xw);

System.Console.WriteLine("xml Create-StringWriter=" + sw.ToString());
}
catch (Exception e)
{
System.Console.WriteLine("xml Create-StringWriter=exception = " +
e.Message);
}
}

static void Case_XmlTextWriter()
{
System.IO.StringWriter sw = new System.IO.StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);

WriteXml(xw);

System.Console.WriteLine("xml XmlTextWriter=" + sw.ToString());
}

static void WriteXml(XmlWriter xw)
{
xw.WriteStartElement("document");

xw.WriteAttributeString("xsi:nil", "true");

xw.WriteEndElement();
xw.Flush();
xw.Close();
}

}
}

Feb 15 '06 #1
1 2919
jschell wrote:
The Case_Create_StringWriter throws an exception [...]

static void Case_Create_StringWriter() {
System.IO.StringWriter sw = new System.IO.StringWriter();
XmlWriter xw = XmlWriter.Create(sw);
WriteXml(xw);
}

static void WriteXml(XmlWriter xw) {
xw.WriteStartElement("document");
xw.WriteAttributeString("xsi:nil", "true");
xw.WriteEndElement();
xw.Flush();
xw.Close();
}


The problem is that you can't specify a namespace using the ns:name syntax
directly in this way. Instead, use an overload of WriteAttributeString()
which explicitly takes both a namespace and name:

xw.WriteAttributeString("xsi","nil","http://uri.of.xsi.namespace/","true");

The reason that your original code mysteriously works for XmlTextWriter but
not XmlWriter is that XmlWriter sets the CheckCharacters attribute of its
XmlWriterSettings instance to true by default, which causes it to check for
invalid characters that XmlTextWriter does not have the ability to check
for. You can read more here:

http://msdn2.microsoft.com/en-us/library/kkz7cs0d.aspx

I hope this helps.
--
Derrick Coetzee, MCAD, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Feb 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

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.