如何使用 XmlSerializer 指定 XmlAttributes 的顺序

发布于 2024-08-28 17:49:53 字数 659 浏览 7 评论 0原文

XmlElement 具有“Order” 属性,在使用 XmlSerializer 进行序列化时,您可以使用该属性指定属性的精确顺序(无论如何都相互关联)。

public class bookingList
{
    [XmlElement(Order = 1)]
    public string error { get; set; }
    [XmlElement(Order = 2)]
    public int counter { get; set; }
    [XmlElement(ElementName = "booking", Order = 3)]
    public List<booking> bookings = new List<booking>();
}

XmlAttribute 有类似的东西吗?我只想将属性的顺序设置为从类似

<MyType end="bob" start="joe" />

<MyType start="joe" end="bob" />

这只是为了可读性,这确实是我自己的好处。

XmlElement has an "Order" attribute which you can use to specify the precise order of your properties (in relation to each other anyway) when serializing using XmlSerializer.

public class bookingList
{
    [XmlElement(Order = 1)]
    public string error { get; set; }
    [XmlElement(Order = 2)]
    public int counter { get; set; }
    [XmlElement(ElementName = "booking", Order = 3)]
    public List<booking> bookings = new List<booking>();
}

Is there a similar thing for XmlAttribute? I just want to set the order of the attributes from something like

<MyType end="bob" start="joe" />

to

<MyType start="joe" end="bob" />

This is just for readability, my own benefit really.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

美人如玉 2024-09-04 17:49:53

不需要,因为属性在 XML 中没有顺序(XML 建议的第 3.1 节说:“请注意,开始标记或空元素标记中属性规范的顺序并不重要。”)。

You don't, as attributes have no order in XML (section 3.1 of the XML recommendation says: "Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.").

时间你老了 2024-09-04 17:49:53

根据我的经验,属性的序列化顺序与定义公共属性的顺序相同。但是,如果将属性与同一类中的字段组合在一起,例如,

[Serializable()]
public class MyClass
{
   [XmlAttribute("ADoubleProp")]
   public double ADoubleProp { get; set; }

   [XmlAttribute("AnIntField")]
   public int AnIntField = 42;
}

那么字段首先会写为属性,然后才是属性。上面的代码会产生类似这样的结果

<MyClass AnIntField="42" ADoubleProp="0" />

From my experience, the order of serialization of attributes is the same as the order you define your public properties. However, if you combine properties with fields in the same class, e.g.

[Serializable()]
public class MyClass
{
   [XmlAttribute("ADoubleProp")]
   public double ADoubleProp { get; set; }

   [XmlAttribute("AnIntField")]
   public int AnIntField = 42;
}

then the fields get written firsts as attributes and then the properties. The code above will produce something like this

<MyClass AnIntField="42" ADoubleProp="0" />
蹲墙角沉默 2024-09-04 17:49:53

在 C# 中,据我所知,属性的顺序按照它们在类中定义的顺序进行序列化。

在这里查看我对此问题的回答:https://stackoverflow.com/a/21468092/607117

In C#, as far as what I have found, the order of attributes are serialized in the order that they are defined in the class.

See my answer to this question here: https://stackoverflow.com/a/21468092/607117

最终幸福 2024-09-04 17:49:53

如果您动态创建 XML,请尝试更改将属性附加到节点的顺序,它应该可以工作:)

If you are creating the XML dynamically, try changing the order in which you append the attribute to the node and it should work :)

香橙ぽ 2024-09-04 17:49:53

您可以首先序列化为XmlDocument,然后通过删除属性来手动更改属性的顺序然后按照您想要的顺序再次添加它们。

虽然这不使用 .NET 注释/属性,但它使您可以完全控制属性顺序。

我从对类似问题的回答中获取了以下代码:

public static void SortAllAttributes(XmlDocument xmlDocument)
{
    foreach (XmlElement element in xmlDocument.SelectNodes("//*"))
    {
        var attributes = element.Attributes
            .Cast<XmlAttribute>()
            .ToArray();
        
        element.Attributes.RemoveAll();

        foreach (var attribute in attributes
                        .OrderBy(a => a.LocalName) // <-- custom sort order
                        .ThenBy(a => a.Name))
        {
            element.Attributes.Append(attribute);
        }
    }
}

You can first serialize to an XmlDocument and then manually change the order of the attributes by removing them and then adding them again in the order you want.

While this does not make use of .NET annotations/attributes, it gives you full control over the attribute order.

I took the following code from my answer to a similar question:

public static void SortAllAttributes(XmlDocument xmlDocument)
{
    foreach (XmlElement element in xmlDocument.SelectNodes("//*"))
    {
        var attributes = element.Attributes
            .Cast<XmlAttribute>()
            .ToArray();
        
        element.Attributes.RemoveAll();

        foreach (var attribute in attributes
                        .OrderBy(a => a.LocalName) // <-- custom sort order
                        .ThenBy(a => a.Name))
        {
            element.Attributes.Append(attribute);
        }
    }
}
短叹 2024-09-04 17:49:53
xmlNode.Attributes.InsertAfter(newAttribute, refAttribute); 
xmlNode.Attributes.InsertBefore(newAttribute, refAttribute);
xmlNode.Attributes.InsertAfter(newAttribute, refAttribute); 
xmlNode.Attributes.InsertBefore(newAttribute, refAttribute);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文