Learning all the time...awesome thanks!
-----Original Message-----
From: [email protected] [mailto:[email protected]]
On Behalf Of Bill McCarthy
Sent: Tuesday, 7 February 2012 10:48 AM
To: 'ozDotNet'
Subject: RE: XML Question
I'm guessing form the original post, Anthony is using VB, as he was using
xml literal syntax in part. The VB version would be
Dim doc = <doc><Credential domain="yyyS">
<Identity>7118</Identity>
</Credential>
<Credential domain="CompanyName">
<Identity> Services Pty Ltd</Identity>
</Credential>
<Credential domain="ABN">
<Identity>123456</Identity>
</Credential>
<Credential domain="BranchID2">
<Identity>22270</Identity>
</Credential>
<Credential domain="Name">
<Identity>ZZ</Identity>
</Credential>
</doc>
Dim item = (From el In doc...<Credential> Where el.@domain =
"BranchID").FirstOrDefault
Dim value = If(item Is Nothing, "00000", item.<Identity>.Value)
I used FirstOrDefault rather than SingleOrDefault; it would depend on your
business rules if you want to take just the first match (First) or throw an
exception if there is more than one match (Single)
|-----Original Message-----
|From: [email protected] [mailto:ozdotnet-
|[email protected]] On Behalf Of Al Gonzalez
|Sent: Tuesday, 7 February 2012 9:14 AM
|To: [email protected]
|Subject: Re: XML Question
|
|Is this what you are trying to do:
|
|static void TestLinq2XMLSelectUsingAttributeValue()
|{
| XElement credentials = new XElement("Credentials",
| MakeCredentialElement("yyyS", "7118"),
| MakeCredentialElement("CompanyName", "Services Pty Ltd"),
| MakeCredentialElement("ABN", "123456"),
| MakeCredentialElement("BranchID", "22270"),
| MakeCredentialElement("Name", "ZZ"));
| var branchId = credentials.Descendants("Credential").Where(c =>
|c.Attribute("domain").Value == "BranchID").SingleOrDefault();
|
| if (branchId != null)
| Console.WriteLine(branchId.Value);
| else
| Console.WriteLine("not found");
|}
|
|static XElement MakeCredentialElement(string domain, string identity) {
| return new XElement("Credential", new XAttribute("domain", domain),
|identity); }
|
|writes out 22270
|
|-----Original Message-----
|From: [email protected]
|Sent: 2/6/2012 4:00 PM
|
|
| <Credential domain="yyyS">
|
| <Identity>7118</Identity>
|
| </Credential>
|
| <Credential domain="CompanyName">
|
| <Identity> Services Pty Ltd</Identity>
|
| </Credential>
|
| <Credential domain="ABN">
|
| <Identity>123456</Identity>
|
| </Credential>
|
| <Credential domain="BranchID">
|
| <Identity>22270</Identity>
|
| </Credential>
|
| <Credential domain="Name">
|
| <Identity>ZZ</Identity>
|
| </Credential>
|
|
|
|
|
| How do i get the SBranchID? I can use
|Linq.XElement.Parse(rt2).<From>.<Credential>(3).<Identity>.Value but what
if i
|do not know the sequence?
|
|
|
|
|
| Anthony
|