简体   繁体   中英

Cannot output <xml> tag when overriding the XmlTextWriter

I've created my own XmlTextWriter from another example, so that I can remove namespaces.

public class NoNamespaceXmlWriter : XmlTextWriter
{
    //Provide as many contructors as you need
    public NoNamespaceXmlWriter(System.IO.TextWriter output)
        : base(output) { Formatting = System.Xml.Formatting.Indented; }

    public override void WriteStartDocument() {}

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        base.WriteStartElement("", localName, "");
    }
}

When I use this it works OK but for some reason it skips the <xml ... > </xml> tags. WriteStartDocument() is never called.

I can't specify settings properly since they have a private setter and usually you use the static method XmlWriter.Create which I cannot override. I tried overriding the settings themselves, but no avail:

public override XmlWriterSettings Settings
{
    get { return new XmlWriterSettings() {OmitXmlDeclaration = false}; }
}

my code to serialize is:

using (StringWriter textWriter = new StringWriter())
{
    using (NoNamespaceXmlWriter xmlWriter = new NoNamespaceXmlWriter(textWriter))
    {
        xs.Serialize(xmlWriter, p);
    }
    xml = textWriter.ToString();
}

Any idea how I can get the xml tag to appear, or why it's disappearing?

If the goal here is to remove the namespaces from the serialized XML, I'd suggest an easier way: You can specify XmlSerializerNamespaces with empty namespace as illustrade in the sample application below

class Program
{
    static void Main(string[] args)
    {
        Player player = new Player() { Id = 102, FirstName = "Danny", LastName = "TopScorer", AverageGoalsPerGame = 3.5, TotalGoalsScored = 150 };

        XmlSerializer serializer = new XmlSerializer(typeof(Player));
        XmlWriterSettings settings = new XmlWriterSettings() { OmitXmlDeclaration = true, Indent = true, Encoding = Encoding.UTF8 };
        StringBuilder output = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(output, settings);

        XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
        xns.Add(string.Empty, string.Empty);
        serializer.Serialize(writer, player, xns);

        Console.WriteLine(output.ToString());
        Console.ReadLine();
    }
}

public class Player
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int TotalGoalsScored { get; set; }
    public double AverageGoalsPerGame { get; set; }
}

It will output:

<Player>
  <Id>102</Id>
  <FirstName>Danny</FirstName>
  <LastName>TopScorer</LastName>
  <TotalGoalsScored>150</TotalGoalsScored>
  <AverageGoalsPerGame>3.5</AverageGoalsPerGame>
</Player>

Otherwise the Player element would look like this with the defaults:

<Player xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://
www.w3.org/2001/XMLSchema">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM