How to remove empty namespace attribute on manually added xml string when serializing object?

天涯浪子 提交于 2019-12-12 01:57:44

问题


I am using XmlSerializer to output my object model to XML. Everything works very well but now I need to add several lines of pre-built XML to the object without building classes for each line. After lots of searching, I found that I can convert the xml string to an XmlElement using XmlDocument's LoadXml and DocumentElement calls. I get the XML I want except that the string section has an empty namespace. How can I eliminate the empty namespace attribute? Is there a better way to add an xml string to the object and have it be serialized properly?

Note: I am only creating output so I don't need to deserialize the generated XML. I am fairly new to the C#, .NET world, and hence, XmlSerialize.

Here is my code:

public class Book
{
    public string Title { get; set; }

    public string Author { get; set; }

    public XmlElement Extension { get; set; }

    public Book()
    {
    }

    public void AddExtension()
    {
        string xmlString = "<AdditionalInfo>" +
                                "<SpecialHandling>Some Value</SpecialHandling>" +
                           "</AdditionalInfo>";

        this.Extension = GetElement(xmlString);
    }

    public static XmlElement GetElement(string xml)
    {
        XmlDocument doc = new XmlDocument();

        doc.LoadXml(xml);

        return doc.DocumentElement;
    }
}

static void Main(string[] args)
{
    TestSerialization p = new TestSerialization();

    Book bookOne = new Book();

    bookOne.Title = "How to Fix Code";
    bookOne.Author = "Dee Bugger";

    bookOne.AddExtension();

    System.Xml.Serialization.XmlSerializer serializer = new XmlSerializer(typeof(Book), "http://www.somenamespace.com");

    using (var writer = new StreamWriter("C:\\BookReport.xml"))
    using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true }))
    {
        serializer.Serialize(xmlWriter, bookOne);
    }
}

Here is my output:

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.somenamespace.com">
  <Title>How to Fix Code</Title>
  <Author>Dee Bugger</Author>
  <Extension>
    <AdditionalInfo xmlns="">
      <SpecialHandling>Some Value</SpecialHandling>
    </AdditionalInfo>
  </Extension>
</Book>

It is the xmlns="" on AdditionalInfo that I want to eliminate. I believe this coming out because there is no association between the XmlDocument I created and the root serialized object, so the XmlDocument creates its own namespace. How can I tell the XmlDocument (and really, the generated XmlElement) that it belongs to the same namespace as the serialized object?


回答1:


This is added because the parent elements have a namespace and your AdditionalInfo element does not. The xmlns="" attribute changes the default namespace for that element and its children.

If you want to get rid of it, then presumably you want the AdditionalInfo element to have the same namespace as its parent. In which case, you need to change your XML to this:

string xmlString = @"<AdditionalInfo xmlns=\"http://www.somenamespace.com\">" +
                    "<SpecialHandling>Some Value</SpecialHandling>" +
                    "</AdditionalInfo>";


来源:https://stackoverflow.com/questions/38727461/how-to-remove-empty-namespace-attribute-on-manually-added-xml-string-when-serial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!