473,563 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Net 2.0 XmlWriter Create and Attributes

The following two cases behave differently in Net 2.0. The
Case_Create_Str ingWriter throws an exception while the Case_XmlTextWri ter
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_XmlTextWri ter();
Case_Create_Str ingWriter();
}

static void Case_Create_Str ingWriter()
{
try
{
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();

XmlWriter xw = XmlWriter.Creat e(sw);

WriteXml(xw);

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

static void Case_XmlTextWri ter()
{
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
XmlTextWriter xw = new XmlTextWriter(s w);

WriteXml(xw);

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

static void WriteXml(XmlWri ter xw)
{
xw.WriteStartEl ement("document ");

xw.WriteAttribu teString("xsi:n il", "true");

xw.WriteEndElem ent();
xw.Flush();
xw.Close();
}

}
}

Feb 15 '06 #1
1 3006
jschell wrote:
The Case_Create_Str ingWriter throws an exception [...]

static void Case_Create_Str ingWriter() {
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
XmlWriter xw = XmlWriter.Creat e(sw);
WriteXml(xw);
}

static void WriteXml(XmlWri ter xw) {
xw.WriteStartEl ement("document ");
xw.WriteAttribu teString("xsi:n il", "true");
xw.WriteEndElem ent();
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 WriteAttributeS tring()
which explicitly takes both a namespace and name:

xw.WriteAttribu teString("xsi", "nil","http ://uri.of.xsi.name space/","true");

The reason that your original code mysteriously works for XmlTextWriter but
not XmlWriter is that XmlWriter sets the CheckCharacters attribute of its
XmlWriterSettin gs 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
8031
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 to check some values during that time also so I was going to be using XmlReader anyway. Unfortunately I don't see an easy way to stream it back out...
4
4171
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 attributes of <html>. When I then insert new XmlNodes into the DOM the HTML tags all end up getting an unnecesarry 'xmlns' attribute added, elements with...
1
1626
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 will be opened with Excel on client machines. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"...
12
1547
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 using XmlWriter, there is one line that I can't figure out how to generate: <ServicesCertifiedMail="OFF" DeliveryConfirmation="ON" ></Services> I've...
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.