Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
<PKT> <Result Name="GetBalance" Success="1"> <Returnset> <Balance Type="int" Value="0" /> </Returnset> </Result> </PKT>
Best way to get the value of Balance with LINQ-to-XML?
Balance
var values = from e in XDocument.Load("MyFile.xml").Descendants("Balance") select e.Attribute("Value").Value; foreach (var e in values) { Console.WriteLine(e); }
Add a comment
XDocument doc = XDocument.Load("MyFile.xml"); IEnumerable<XElement> elements = doc.Descendants("Balance"); foreach (XElement e in elements) { Console.Write(e.Attribute("Value").Value); }
You can do it this way. I typed the code directly here, you may want to confirm any typos.
If you wanted to only get the value from the first occurrence of Balance, you could do.
var balance = (from n in XDocument.Load("MyFile.xml").Descendents("Balance") select n.Attributes("Value").Value).ToList().First();
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.