473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2982
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Malinak | last post by:
Since XmlWriter offers so many nice options for formatting, I thought it would be nice to read in via XmlReader, and write back out via XmlWriter. It might be overkill, but I'd also like to be able...
4
by: David Grogan | last post by:
Hi, 2 questions.... 1. I'm parsing an XHTML document that contains both the default namespace (xmlns="http://www.w3.org/1999/xhtml") and a custom one (xmlns:r="...") - both of these being...
1
by: Paul | last post by:
Hi I am trying to create the attributes shown below using the xmlwriter, just wondering if anyone has any ideas how to accomplish this? I am creating a xml file in a dot.net web application that...
12
by: sklett | last post by:
**Disclaimer** The element I need to create might not be valid XML, but it's not my fault, I'm following a required format for an integration project. I've got 99% of the XML created nicely...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.