XmlAttribute顺序或序列在序列化.NET类时出现问题。

huangapple go评论60阅读模式
英文:

XmlAttribute order or sequence going wrong while serializing .net class

问题

I have an existing class where we have many public properties. Most of them are decorated with [XmlAttribute]. Issue is when I have 16 member then xml show order of attribute correctly

class Market
{
    [XmlArray(Order = 0)]
    [XmlArrayItem(IsNullable = false)]
    public List<Option> Options { get; set; }

    [XmlElement(Order = 1)]
    public MarketType MarketType { get; set; }

    [XmlAttribute]
    public int Id {get;set;}

    [XmlAttribute]
    public string Name {get;set;}

    [XmlAttribute]
    public bool IsEnabled {get;set;}
    .
    .
    .
    // like this 17 XML attribute are there
}
<Market Id="10" Name="Test" IsEnabled="false"  ...  a="" b="" c="" d="">

When I have 17 attribute then it brings 3rd Last and 4th Last attribute in the start and 1st properties goes to 3rd last and 4th last position, i.e. order got disturbed. I tried shuffling element, it only swaps 1st 2 attribute with 3rd and 4th last attribute.

<Market b="" a="" IsEnabled="false"  ... Name="Test" Id="10"  c="" d="">
英文:

I have a existing class where we have many public properties. Most of them are decorated with [XmlAttribute]. Issue is when I have 16 member then xml show order of attribute correctly

class Market
{
    [XmlArray(Order = 0)]
    [XmlArrayItem(IsNullable = false)]
    public List&lt;Option&gt; Options { get; set; }

    [XmlElement(Order = 1)]
    public MarketType MarketType { get; set; }

    [XmlAttribute]
    public int Id {get;set;}

    [XmlAttribute]
    public string Name {get;set;}

    [XmlAttribute]
    public bool IsEnabled {get;set;}
        .
        .
        .
    // like this 17 XML attribute are there
}
&lt;Market Id=&quot;10&quot; Name=&quot;Test&quot; IsEnabled=&quot;false&quot;  ...  a=&quot;&quot; b=&quot;&quot; c=&quot;&quot; d=&quot;&quot;&gt;

When I have 17 attribute then it brings 3rd Last and 4th Last attribute in the start and 1st properties goes to 3rd last and 4th last position, i.e. order got disturbed. I tried shuffling element, it only swaps 1st 2 attribute with 3rd and 4th last attribute.

&lt;Market b=&quot;&quot; a=&quot;&quot; IsEnabled=&quot;false&quot;  ... Name=&quot;Test&quot; Id=&quot;10&quot;  c=&quot;&quot; d=&quot;&quot;&gt;

答案1

得分: 3

XML中不能依赖属性的顺序:https://stackoverflow.com/a/57757267/20771004

如果顺序很重要,您可能希望将它们改为元素。

英文:

You can’t rely on the order of attributes in XML: https://stackoverflow.com/a/57757267/20771004

If order is important, you might want to make them elements instead.

答案2

得分: 1

使用IXmlSerializable进行自定义序列化

class Market : IXmlSerializable
{
    [XmlAttribute]
    public int Id { get; set; }

    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public bool IsEnabled { get; set; }

    // 类似这样的 17 个 XML 属性
    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("Id", Id.ToString());
        writer.WriteAttributeString("Name", Name);
        writer.WriteAttributeString("IsEnabled", IsEnabled.ToString());
    }

    public void ReadXml(XmlReader reader)
    {
        XElement element = (XElement)XElement.ReadFrom(reader);
        foreach (XElement node in element.Elements())
        {
        }
        foreach (XAttribute attribute in element.Attributes())
        {
            switch (attribute.Name.LocalName)
            {
                case "Id":
                    Id = (int)attribute;
                    break;
                case "Name":
                    Name = (string)attribute;
                    break;
                case "IsEnabled":
                    IsEnabled = (Boolean)attribute;
                    break;
            }
        }
    }

    public XmlSchema GetSchema()
    {
        return (null);
    }
}
英文:

Use a custom serialization using IXmlSerializable

<!-- begin snippet: js hide: false console: true babel: false -->

    class Market : IXmlSerializable
    {

        [XmlAttribute]
        public int Id {get;set;}

        [XmlAttribute]
        public string Name {get;set;}

        [XmlAttribute]
        public bool IsEnabled {get;set;}
 
        // like this 17 XML attribute are there
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString(&quot;Id&quot;, Id.ToString());
            writer.WriteAttributeString(&quot;Name&quot;, Name);
            writer.WriteAttributeString(&quot;IsEnabled&quot;, IsEnabled.ToString());
        }

        public void ReadXml(XmlReader reader)
        {
            XElement element = (XElement)XElement.ReadFrom(reader);
            foreach(XElement node in element.Elements())
            {
            }
            foreach (XAttribute attribute in element.Attributes())
            {
                switch (attribute.Name.LocalName)
                {
                    case &quot;Id&quot; :
                        Id = (int)attribute;
                        break;
                    case &quot;Name&quot;:
                        Name  = (string)attribute;
                        break;
                    case &quot;IsEnabled&quot;:
                        IsEnabled = (Boolean)attribute;
                        break;
                }
            }
        }

        public XmlSchema GetSchema()
        {
            return (null);
        }
    }

<!-- end snippet -->

答案3

得分: 0

Here is the translated code:

找到了正确的解决方案,我从 XmlArray  XmlElement 中移除了 order,然后它开始正常工作。
现在我的类看起来像这样:

class Market
{
    [XmlArray]
    [XmlArrayItem(IsNullable = false)]
    public List<Option> Options { get; set; }

    [XmlElement]
    public MarketType MarketType { get; set; }

    [XmlAttribute]
    public int Id { get; set; }

    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public bool IsEnabled { get; set; }
    .
    .
    .
    // 这样一共有 17 个 XML 属性
}
英文:

Got a proper solution, I removed order from XmlArray and XmlElement and it started working fine,
Now my class looks like this

class Market
{
    [XmlArray]
    [XmlArrayItem(IsNullable = false)]
    public List&lt;Option&gt; Options { get; set; }

    [XmlElement]
    public MarketType MarketType { get; set; }

    [XmlAttribute]
    public int Id {get;set;}

    [XmlAttribute]
    public string Name {get;set;}

    [XmlAttribute]
    public bool IsEnabled {get;set;}
    .
    .
    .
    // like this 17 XML attribute are there
}

huangapple
  • 本文由 发表于 2023年6月8日 18:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定