I have the following XML code:
<dsPreventieRegisterItem xmlns="http://tempuri.org/dsPreventieRegisterItem.xsd">
<tblpreventieregisteritem>
<DatumInterventie>2015-06-14</DatumInterventie>
<TijdstipInterventie>11:30</TijdstipInterventie>
<HulpverlenerNaam>Vandenbroucke</HulpverlenerNaam>
<HulpverlenerVoornaam>Dirk</HulpverlenerVoornaam>
<HulpbehoefteAard>Vinger en pols bezeerd </HulpbehoefteAard>
<HulpbehoefteOorzaak>Ronddraaiende delen</HulpbehoefteOorzaak>
<GebodenHulp>aanbrengen van Cold pack </GebodenHulp>
<GebruikteMiddelen>Cold pack Ice spray Steunverband</GebruikteMiddelen>
<Opmerkingen />
<id>1</id>
</tblPreventieRegisterItem>
</tblpreventieregisteritem>
But with multiple tblPreventieRegisterItem elements. This is declared in an XElement.
And then i want to put it in a IEnumerable<XElement>
list. The problem i'm having is that the orderby never works.
IEnumerable <XElement> items =
from el in root.Elements("tblpreventieregisteritem")
orderby Int32.Parse(el.Element("id").Value) descending
select el;
Anyone who has an idea?
This is a FAQ (Frequently Asked Question) in the XML topic. All elements in your XML are in default namespace which URI is "http://tempuri.org/dsPreventieRegisterItem.xsd"
. You can use combination of XNamespace+ element's local-name
to reference element in namespace, for example :
XNamespace d = "http://tempuri.org/dsPreventieRegisterItem.xsd";
IEnumerable <XElement> items =
from el in root.Elements(d+"tblpreventieregisteritem")
orderby (int)el.Element(d+"id") descending
select el;
Notice that you can just cast XElement
directly to int
, which is a more concise and safer approach ie in case the XElement
in question is not found.
var xml = @"<dsPreventieRegisterItem xmlns='http://tempuri.org/dsPreventieRegisterItem.xsd'>
<tblpreventieregisteritem>
<id>1</id>
</tblpreventieregisteritem>
<tblpreventieregisteritem>
<id>2</id>
</tblpreventieregisteritem>
<tblpreventieregisteritem>
<id>3</id>
</tblpreventieregisteritem>
</dsPreventieRegisterItem> ";
var root = XElement.Parse(xml);
XNamespace d = "http://tempuri.org/dsPreventieRegisterItem.xsd";
IEnumerable<XElement> items =
from el in root.Elements(d + "tblpreventieregisteritem")
orderby (int)el.Element(d + "id") descending
select el;
foreach (var item in items)
{
Console.WriteLine(item.Element(d + "id").ToString());
}
output :
<id xmlns="http://tempuri.org/dsPreventieRegisterItem.xsd">3</id>
<id xmlns="http://tempuri.org/dsPreventieRegisterItem.xsd">2</id>
<id xmlns="http://tempuri.org/dsPreventieRegisterItem.xsd">1</id>
Have you tried this one:
XNamespace ns = "http://tempuri.org/dsPreventieRegisterItem.xsd";
IEnumerable<XElement> items =
from rootElement in root.Elements("{" + ns + "}" + "dsPreventieRegisterItem")
from el in rootElement.Elements("{" + ns + "}" + "tblpreventieregisteritem")
orderby Int32.Parse(el.Element("{" + ns + "}" + "id").Value) descending
select el;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.