Query XML with LINQ
-
Hi, I've the following XML-
<MessageParts xmlns="http://www.address.com">
<Status xmlns="">
.
.
.
.
<Data>
<PersonsMin xmlns="some attribute">
.
.
.
<Parts>
.
.
.
</Parts>
</PersonsMin>
</Data>
</Status>
</MessageParts>I'me trying to get all the elements in PersonsMin.
var query = from response in xmlDoc.Elements("PersonsMin") select response;
But i'm getting "Sequence contains no elements". What i'm doing wrong? -
Hi, I've the following XML-
<MessageParts xmlns="http://www.address.com">
<Status xmlns="">
.
.
.
.
<Data>
<PersonsMin xmlns="some attribute">
.
.
.
<Parts>
.
.
.
</Parts>
</PersonsMin>
</Data>
</Status>
</MessageParts>I'me trying to get all the elements in PersonsMin.
var query = from response in xmlDoc.Elements("PersonsMin") select response;
But i'm getting "Sequence contains no elements". What i'm doing wrong?treuveni wrote:
What i'm doing wrong?
That's fairly simple, you've forgotten to add the XML namespace to the query. Typically you would do this using
XNamespace myNamespace = "some namespace";
var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin")
select response;Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
treuveni wrote:
What i'm doing wrong?
That's fairly simple, you've forgotten to add the XML namespace to the query. Typically you would do this using
XNamespace myNamespace = "some namespace";
var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin")
select response;Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Pete O'Hanlon wrote:
you've forgotten to add the XML namespace to the query
Like this?
XNamespace myNamespace = "some attribute"; var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin") select response;
XNamespace is the XAttribute?Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Pete O'Hanlon wrote:
Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.
Still same error "Sequence contains no elements"
Try this:
var query = from response in xDoc.Descendants(ns + "PersonsMin")
select response;Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility