472,090 Members | 1,275 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,090 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 9135
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Malinak | last post: by
1 post views Thread by jschell | last post: by
2 posts views Thread by Harry | last post: by
1 post views Thread by MAF | last post: by
9 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by leo001 | last post: by

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.