Reputation: 5021
Though it seems to be a very trivial question I am trying to understand XMLReader Class and it's members. Given a XML file I want to opt out of printing XML Declaration
static void Main(string[] args)
{
StringBuilder output = new StringBuilder();
String xmlString =
@"<?xml version='1.0'?>
<!-- This is a sample XML document -->
<Items>
<Item>test with a child element <more/> stuff</Item>
</Items>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
}
}
Console.WriteLine( output.ToString());
Console.ReadKey();
}
But if I comment out
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
I still get to see the XML Declaration i.e.
<?xml version='1.0'?>
What's wrong here ? Again if I comment out
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
it says InvalidOperationException was unhandled . Could you please explain ? I am not getting the whole picture.
Upvotes: 0
Views: 129
Reputation: 26213
The writing of the declaration is a feature of the XmlWriter
. You can opt out of this using XmlWriterSettings
.
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true
};
using (XmlWriter writer = XmlWriter.Create(output, settings))
See the documentation for full details of all settings available.
Upvotes: 1
Reputation: 1319
Set ws.OmitXmlDeclaration = true;
in your code to omit xml declaration.
static void Main(string[] args)
{
StringBuilder output = new StringBuilder();
String xmlString =
@"<?xml version='1.0'?>
<!-- This is a sample XML document -->
<Items>
<Item>test with a child element <more/> stuff</Item>
</Items>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.OmitXmlDeclaration = true;
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
//case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
}
}
Console.WriteLine( output.ToString());
Console.ReadKey();
}
Upvotes: 2