RE: XML Question
Learning all the time...awesome thanks! -Original Message- From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] 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 = 7118 Services Pty Ltd 123456 22270 ZZ Dim item = (From el In doc... Where el.@domain = "BranchID").FirstOrDefault Dim value = If(item Is Nothing, "0", item..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: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet- |boun...@ozdotnet.com] On Behalf Of Al Gonzalez |Sent: Tuesday, 7 February 2012 9:14 AM |To: ozdotnet@ozdotnet.com |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: ifum...@gmail.com |Sent: 2/6/2012 4:00 PM | | | | | 7118 | | | | | |Services Pty Ltd | | | | | | 123456 | | | | | | 22270 | | | | | | ZZ | | | | | | | | How do i get the SBranchID? I can use |Linq.XElement.Parse(rt2)..(3)..Value but what if i |do not know the sequence? | | | | | | Anthony |
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 = 7118 Services Pty Ltd 123456 22270 ZZ Dim item = (From el In doc... Where el.@domain = "BranchID").FirstOrDefault Dim value = If(item Is Nothing, "0", item..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: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet- |boun...@ozdotnet.com] On Behalf Of Al Gonzalez |Sent: Tuesday, 7 February 2012 9:14 AM |To: ozdotnet@ozdotnet.com |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: ifum...@gmail.com |Sent: 2/6/2012 4:00 PM | | | | | 7118 | | | | | |Services Pty Ltd | | | | | | 123456 | | | | | | 22270 | | | | | | ZZ | | | | | | | | How do i get the SBranchID? I can use |Linq.XElement.Parse(rt2)..(3)..Value but what if i |do not know the sequence? | | | | | | Anthony |
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: ifum...@gmail.com Sent: 2/6/2012 4:00 PM 7118 Services Pty Ltd 123456 22270 ZZ How do i get the SBranchID? I can useLinq.XElement.Parse(rt2)..(3)..Value but what if i do not know the sequence? Anthony
Re: XML Question
No worries, Enjoy. Also you might want to check out http://stackoverflow.com/ for more goodies. On 7 February 2012 08:48, wrote: > Thank you J > > ** ** > > *From:* ozdotnet-boun...@ozdotnet.com [mailto: > ozdotnet-boun...@ozdotnet.com] *On Behalf Of *Arjang Assadi > *Sent:* Tuesday, 7 February 2012 8:29 AM > > *To:* ozDotNet > *Subject:* Re: XML Question > > ** ** > > http://msdn.microsoft.com/en-us/library/bb387086.aspx > > On 7 February 2012 08:25, wrote: > > Any code on how i would do this, fairly new to linq > > > > *From:* ozdotnet-boun...@ozdotnet.com [mailto: > ozdotnet-boun...@ozdotnet.com] *On Behalf Of *Arjang Assadi > *Sent:* Tuesday, 7 February 2012 8:23 AM > *To:* ozDotNet > *Subject:* Re: XML Question > > > > Rather than working by index, look at the attribute value for > Credential.domain, > > ignore the Credentials where the "domain" attribute is not BranchID. > > On 7 February 2012 08:00, wrote: > > > > 7118 > > > > > > Services Pty Ltd > > > > > > 123456 > > > > > > 22270 > > > > > > ZZ > > > > > > > > How do i get the SBranchID? I can use > Linq.XElement.Parse(rt2)..(3)..Value but what if > i do not know the sequence? > > > > > > Anthony > > > > ** ** >
RE: XML Question
Thank you J From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Arjang Assadi Sent: Tuesday, 7 February 2012 8:29 AM To: ozDotNet Subject: Re: XML Question http://msdn.microsoft.com/en-us/library/bb387086.aspx On 7 February 2012 08:25, wrote: Any code on how i would do this, fairly new to linq From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Arjang Assadi Sent: Tuesday, 7 February 2012 8:23 AM To: ozDotNet Subject: Re: XML Question Rather than working by index, look at the attribute value for Credential.domain, ignore the Credentials where the "domain" attribute is not BranchID. On 7 February 2012 08:00, wrote: 7118 Services Pty Ltd 123456 22270 ZZ How do i get the SBranchID? I can use Linq.XElement.Parse(rt2)..(3)..Value but what if i do not know the sequence? Anthony
Re: XML Question
http://msdn.microsoft.com/en-us/library/bb387086.aspx On 7 February 2012 08:25, wrote: > Any code on how i would do this, fairly new to linq > > ** ** > > *From:* ozdotnet-boun...@ozdotnet.com [mailto: > ozdotnet-boun...@ozdotnet.com] *On Behalf Of *Arjang Assadi > *Sent:* Tuesday, 7 February 2012 8:23 AM > *To:* ozDotNet > *Subject:* Re: XML Question > > ** ** > > Rather than working by index, look at the attribute value for > Credential.domain, > > ignore the Credentials where the "domain" attribute is not BranchID. > > On 7 February 2012 08:00, wrote: > > > > 7118 > > > > > > Services Pty Ltd > > > > > > 123456 > > > > > > 22270 > > > > > > ZZ > > > > > > > > How do i get the SBranchID? I can use > Linq.XElement.Parse(rt2)..(3)..Value but what if > i do not know the sequence? > > > > > > Anthony > > ** ** >
RE: XML Question
Any code on how i would do this, fairly new to linq From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On Behalf Of Arjang Assadi Sent: Tuesday, 7 February 2012 8:23 AM To: ozDotNet Subject: Re: XML Question Rather than working by index, look at the attribute value for Credential.domain, ignore the Credentials where the "domain" attribute is not BranchID. On 7 February 2012 08:00, wrote: 7118 Services Pty Ltd 123456 22270 ZZ How do i get the SBranchID? I can use Linq.XElement.Parse(rt2)..(3)..Value but what if i do not know the sequence? Anthony
Re: XML Question
Rather than working by index, look at the attribute value for Credential.domain, ignore the Credentials where the "domain" attribute is not BranchID. On 7 February 2012 08:00, wrote: > > > 7118 > > > > > > Services Pty Ltd > > > > > > 123456 > > > > > > 22270 > > > > > > ZZ > > > > ** ** > > ** ** > > How do i get the SBranchID? I can use > Linq.XElement.Parse(rt2)..(3)..Value but what if > i do not know the sequence? > > ** ** > > ** ** > > Anthony >