473,324 Members | 2,313 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,324 software developers and data experts.

XmlWriter with ConformanceLevel.Fragment

Hi
I keep getting error :

WriteStartDocument cannot be called on writers created with
ConformanceLevel.Fragment.

The code that I have is very simple.

SimpleClass c = new SimpleClass();

c.Name = "TestName";

XmlSerializer serializer = new XmlSerializer(typeof(SimpleClass));
MemoryStream stream =new MemoryStream();

XmlWriterSettings settings = new XmlWriterSettings();

settings.OmitXmlDeclaration = true;

settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.Encoding = Encoding.ASCII;

XmlWriter writer = XmlWriter.Create(stream,settings);

serializer.Serialize(writer,c);

stream.Position = 0;

byte[] data=new byte[stream.Length];

stream.Read(data, 0, (int)stream.Length);
result = new string(Encoding.Default.GetChars(data));

I found in documentation that if there is DTD information that
WriteStartDocument will be called and with ConformanceLevel.Fragment writer
will throw an exception.

So where is DTD information? Can I remove it to make this work?

Thanks.

Shimon
May 8 '07 #1
2 9307
Shimon Sim wrote:
I keep getting error :

WriteStartDocument cannot be called on writers created with
ConformanceLevel.Fragment.

The code that I have is very simple.

SimpleClass c = new SimpleClass();

c.Name = "TestName";

XmlSerializer serializer = new XmlSerializer(typeof(SimpleClass));
MemoryStream stream =new MemoryStream();

XmlWriterSettings settings = new XmlWriterSettings();

settings.OmitXmlDeclaration = true;

settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.Encoding = Encoding.ASCII;

XmlWriter writer = XmlWriter.Create(stream,settings);

serializer.Serialize(writer,c);
I guess the Serialize method of XmlSerializer calls the
WriteStartDocument method of XmlWriter. Why exactly do you set
ConformanceLevel.Fragment, what do you want to achieve with that setting?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 9 '07 #2
I want to serialize class without header and without any default namespaces.
Header could be easily removed as you showed me earlier but to remove
namespaces that doesn't help. I did find one solution to go through
XmlDocument and extract a node from it.
But I wanted to do it directly using XmlWriter. I looked into custom
XmlWriters but didn't see any fast solution. So I thought that if I will use
Fragment it will not add any namespaces.
Thanks,
Shimon.
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:u$**************@TK2MSFTNGP03.phx.gbl...
Shimon Sim wrote:
>I keep getting error :

WriteStartDocument cannot be called on writers created with
ConformanceLevel.Fragment.

The code that I have is very simple.

SimpleClass c = new SimpleClass();

c.Name = "TestName";

XmlSerializer serializer = new XmlSerializer(typeof(SimpleClass));
MemoryStream stream =new MemoryStream();

XmlWriterSettings settings = new XmlWriterSettings();

settings.OmitXmlDeclaration = true;

settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.Encoding = Encoding.ASCII;

XmlWriter writer = XmlWriter.Create(stream,settings);

serializer.Serialize(writer,c);

I guess the Serialize method of XmlSerializer calls the WriteStartDocument
method of XmlWriter. Why exactly do you set ConformanceLevel.Fragment,
what do you want to achieve with that setting?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 10 '07 #3

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...
1
by: jschell | last post by:
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...
2
by: Harry | last post by:
Hi, I am using XMLWriter to build xml and I need to build the element below. How do I do this? <Password format="encrypted">password</Password> Thanks
2
by: Simon Harvey | last post by:
Hi all, I'm having a real problem with getting an XMLWriter as a result of an xsl tranform I'm attempting. My code is: private void btnPerformTransform_Click(object sender, EventArgs e) {...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
1
by: MAF | last post by:
Is there a simple way to get the text, xml, from an xmlwriter. I have a XML writer that writes to a file, and I want another function to return the text that the xml writer is produced. Any...
2
by: darrel | last post by:
I want to make my XML as such: <parentItem> <childItem attribute="myAttribute">myContent</childItem> </parentItem> I thought I could just do this: ...
9
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a .net application and am using the xml writer class to create an xml file that opens as an excel file. I am trying to write out the following but am having difficulty. <Row> <Cell...
1
by: depalau | last post by:
I'm experiencing issues where XmlSerialier.Deserialize throws an exception when attempting to use a MemoryStream built with an XmlWriter vs. a standalone StringReader. It is attempting to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.