How do I change root element name while keeping co

2019-04-24 07:02发布

I have an XML document:

<data>
    <elmt1>Element 1</elmt1>
    <elmnt2>Element 2</elmnt2>
    <elmnt3>Element 3</elmnt3>
</data>

I need to deserialize to an object that serializes to a different root name with everything else remaining the same.

For example:

<dataNew>
    <elmt1>Element 1</elmt1>
    <elmnt2>Element 2</elmnt2>
    <elmnt3>Element 3</elmnt3>
</dataNew>

When serializing, we can always apply XmlRootAttribute to serialize to a different root name but I am not sure how to deserialize to a different XmlRootAttribute. It keeps failing error in document (1,2) pointing to the root attribute.

How can I achieve that?

6条回答
Deceive 欺骗
2楼-- · 2019-04-24 07:24

If it's only the root name you want to change you can specify the root attribute when declaring the XmlSerializer.

XmlSerializer xmlSerializer = new XmlSerializer(typeof(data), new XmlRootAttribute("dataNew"));
查看更多
唯我独甜
3楼-- · 2019-04-24 07:25

XmlRootAttribute was supposed to work

[XmlRoot("dataNew")]
public class MyData()
{
    [XmlElement("elmt1")]
    public string myElement1{get;set;}

    [XmlElement("elmnt2")]
    public string myElement2{get;set;}

    [XmlElement("elmtn3")]
    public string myElement3{get;set;}

}

EDIT: Completed the XML

查看更多
疯言疯语
4楼-- · 2019-04-24 07:44

a sample of using XmlAttributeOverrides. If you vote up give one to hjb417 as well

class Program
{
    static void Main(string[] args)
    {
        using (var fs = File.OpenRead("XmlFile1.xml"))
        using (var fs2 = File.OpenRead("XmlFile2.xml"))
        {
            var xSer = new XmlSerializer(typeof(data));
            var obj = xSer.Deserialize(fs);
        //
            var xattribs = new XmlAttributes();
            var xroot = new XmlRootAttribute("dataNew");
            xattribs.XmlRoot = xroot;
            var xoverrides = new XmlAttributeOverrides();
            xoverrides.Add(typeof(data), xattribs);
            var xSer2 = new XmlSerializer(typeof(data), xoverrides);
            var obj2 = xSer2.Deserialize(fs2);
        }
    }
}

public class data
{
    public string elmt1 { get; set; }
    public string elmnt2 { get; set; }
    public string elmnt3 { get; set; }
}
查看更多
来,给爷笑一个
5楼-- · 2019-04-24 07:44

You can use ExtendedXmlSerializer. This serializer support change root element name and property name. If you have class like this:

[XmlRoot("dataNew")]
public class Data
{
    [XmlElement("elmt1")]
    public string Element1 { get; set; }

    [XmlElement("elmnt2")]
    public string Element2 { get; set; }

    [XmlElement("elmtn3")]
    public string Element3 { get; set; }
}

You can serialize it:

ExtendedXmlSerializer serializer = new ExtendedXmlSerializer();
var obj = new Data
    {
        Element1 = "A",
        Element2 = "B",
        Element3 = "C",
    };
var xml = serializer.Serialize(obj);

Your xml will look like:

<?xml version="1.0" encoding="utf-8"?>
<dataNew type="Models.Example">
    <elmt1>A</elmt1>
    <elmnt2>B</elmnt2>
    <elmtn3>C</elmtn3>
</dataNew>

ExtendedXmlSerializer has many other useful features:

  • Deserialization xml from standard XMLSerializer
  • Serialization class with property interface
  • Serialization circular reference and reference Id
  • Deserialization of old version of xml
  • Property encryption
  • Custom serializer

ExtendedXmlSerializer supports .net 4.5 and .net Core. You can integrate it with WebApi and AspCore.

查看更多
迷人小祖宗
6楼-- · 2019-04-24 07:45

Did you try using the XmlAttributeOverrides class?

查看更多
看我几分像从前
7楼-- · 2019-04-24 07:45

You might have to implement ISerializable and change the root element in GetObjectData().

查看更多
登录 后发表回答