G
Gve
Hi,
I'm building an xml message with the XmlWriter class and process this xml
message with the XmlReader class. The message I build looks like:
<parent>
<child>car</child>
<child>bus</child>
<child>plane</child>
</parent>
When I build the message with XmlWriterSettings and set the
XmlWriterSettings .Indent property to true, I'll get the expected results
when I iterate through the message with the XmlReader class. Nevertheless,
when I build the message with setting the XmlWriterSettings .Indent property
to false, the bus value won't show up.
Is this by design or is it a bug? I've tried framework 2.0 and 3.5.
The following code fragment presents the problem:
//==
using System;
using System.IO;
using System.Xml;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
WriteAndReadXml(true);
WriteAndReadXml(false);
}
static void WriteAndReadXml(bool indent) {
Console.WriteLine("-Indent: {0}", indent);
using(Stream s = new MemoryStream()) {
WriteXml(s, indent);
s.Position = 0;
ReadXml(s);
}
Console.WriteLine("\r\n");
}
static void WriteXml(Stream output, bool indent) {
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = indent;
using(XmlWriter writer = XmlWriter.Create(output, settings)) {
writer.WriteStartDocument();
writer.WriteStartElement("parent");
foreach(string value in new string[] { "car", "bus", "plane" }) {
writer.WriteStartElement("child");
writer.WriteValue(value);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}
static void ReadXml(Stream input) {
using (XmlReader reader = XmlReader.Create(input))
while(reader.ReadToFollowing("child"))
Console.WriteLine(reader.ReadElementContentAsString());
}
}
}
//==
Thanx
Regards
I'm building an xml message with the XmlWriter class and process this xml
message with the XmlReader class. The message I build looks like:
<parent>
<child>car</child>
<child>bus</child>
<child>plane</child>
</parent>
When I build the message with XmlWriterSettings and set the
XmlWriterSettings .Indent property to true, I'll get the expected results
when I iterate through the message with the XmlReader class. Nevertheless,
when I build the message with setting the XmlWriterSettings .Indent property
to false, the bus value won't show up.
Is this by design or is it a bug? I've tried framework 2.0 and 3.5.
The following code fragment presents the problem:
//==
using System;
using System.IO;
using System.Xml;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
WriteAndReadXml(true);
WriteAndReadXml(false);
}
static void WriteAndReadXml(bool indent) {
Console.WriteLine("-Indent: {0}", indent);
using(Stream s = new MemoryStream()) {
WriteXml(s, indent);
s.Position = 0;
ReadXml(s);
}
Console.WriteLine("\r\n");
}
static void WriteXml(Stream output, bool indent) {
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = indent;
using(XmlWriter writer = XmlWriter.Create(output, settings)) {
writer.WriteStartDocument();
writer.WriteStartElement("parent");
foreach(string value in new string[] { "car", "bus", "plane" }) {
writer.WriteStartElement("child");
writer.WriteValue(value);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}
static void ReadXml(Stream input) {
using (XmlReader reader = XmlReader.Create(input))
while(reader.ReadToFollowing("child"))
Console.WriteLine(reader.ReadElementContentAsString());
}
}
}
//==
Thanx
Regards