1

Im new to C# and Im currently working with XML-files. I have sumbled across the XmlTextReader object. I want to sort some attributes In my XML-file by value, but I can't find any easy way to do this with XmlTextReader?

XDocument have the OrderBy-property, but does XmlTextReader have any similar?

XML to sort

<Order>
  <orderdate>1998-04-22T00:00:00</orderdate>
</Order>
<Order>
  <orderdate>1998-04-07T00:00:00</orderdate>
</Order>
<Order>
  <orderdate>1998-04-30T00:00:00</orderdate>
</Order>
11
  • 1
    From the docs XmlTextReader Represents a reader that provides fast, non-cached, forward-only access to XML data. So basically no, it doesn't do sorting, and XDocument is probably your best bet; if you explain your problem in more detail we might be able to give an alternative suggestion. By the way, this class is obsolete, use XmlReader.Create(). Commented Sep 13, 2015 at 18:24
  • I have an xml-file with different different <OrderDate> attributes containing dates. I want to sort this attributes based on their values In ascending order. Commented Sep 13, 2015 at 18:31
  • Why do you want to use the xmltextreader indestead of xdocument? Commented Sep 13, 2015 at 18:33
  • xmltextreader seems to be easy to understand compared to xdocument? But I will give the xdocument a shot. Commented Sep 13, 2015 at 18:34
  • 1
    There are no attributes in sample you've posted - only elements with text values. Also not that in XML order of attributes for an element does not matter - so you question is probably about something else OR you are using term "attribute" in some meaning not related to XML. Commented Sep 13, 2015 at 19:59

1 Answer 1

1

According to the docs, XmlTextReader Represents a reader that provides fast, non-cached, forward-only access to XML data. Thus it isn't really sufficient for sorting XML data, as sorting requires comparing and rearranging elements that are currently out of order. Instead, the XDocument class would be your best bet.

Since you only give a fragment of your XML, it's unclear whether your elements Order and orderdate belong in the default namespace of some parent element. If they do not belong in any namespace, you can use XDocument.Descendants(XName) and LINQ to XML as follows, taking advantage of the implicit conversion of string to XName:

        var items = xdocument.Descendants("Order")
            .OrderBy(o => (DateTime?)o.Element("orderdate"))
            .ToList();

If the elements belong in a namespace specified on the root XML element, you need specify that namespace in your query using XNamespace, from which you can construct a full XName for Order to use in your Descendants query:

        XNamespace ns = xdocument.Root.Name.Namespace;

        var items = xdocument.Descendants(ns + "Order")
            .OrderBy(o => (DateTime?)o.Element(ns + "orderdate"))
            .ToList();

Or if the namespace is specified by some intermediate element rather than the root element, it may be easiest to specify the namespace as a string literal:

        XNamespace ns = "http://MiddleNamespace";

        var items = xdocument.Descendants(ns + "Order")
            .OrderBy(o => (DateTime?)o.Element(ns + "orderdate"))
            .ToList();

Working fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.