-3

I have a .xml that is formatted like this:

<!--List of Locations-->
<Locations>
    <Location Name="House">
        <XCoord>0</XCoord>
        <YCoord>0</YCoord>
        <ZCoord>0</ZCoord>
    </Location>
</Locations>

I want to be able to read the "House" part of it and store it as a string, can anybody help?

Joshe343
  • 65
  • 1
  • 10
  • 2
    What have you tried? I'd suggest [`XDocument`](http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx) as a starting point. – Tim S. Aug 27 '14 at 16:32
  • 1
    I recommend searching about Linq to Xml. – Mephy Aug 27 '14 at 16:37
  • 1
    As a side note it's usually best for you to put your attempted code up for others to work with rather than just asking for an answer. – Cubicle.Jockey Aug 27 '14 at 16:46
  • 2
    Please don't just ask us to solve the problem for you. Show us how _you_ tried to solve the problem yourself, then show us _exactly_ what the result was, and tell us why you feel it didn't work. See "[What Have You Tried?](http://whathaveyoutried.com/)" for an excellent article that you _really need to read_. – John Saunders Aug 27 '14 at 16:52
  • Also, what do you mean "the House part of it"? Just the word "House", or the entire `` element which has `Name="House"`? – John Saunders Aug 27 '14 at 16:55

2 Answers2

1

Below is using XDocument and will grab all the location element below the root element locations and make an IEnumerable with all the names.

var xml = "<Locations><Location Name=\"House\"><XCoord>0</XCoord><YCoord>0</YCoord><ZCoord>0</ZCoord></Location></Locations>";

var xDoc = XDocument.Parse(xml);

var attributes = 
        xDoc.Root //Locations
            .Elements() //Each individual Location
            .Select(element => element.Attribute("Name").Value); //Grab all the name attribute values

attributes variable is an IEnumerable<string> of the name values.

Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
0

Parse the xml with XDocument

XDocument doc = XDocument.Parse(xml);

Select the house elements

IEnumerable<string> nameValues =
            from el in doc.Root.Elements()
            where el.Attribute("Name") != null
            select el.Attribute("Name").Value;
AxdorphCoder
  • 1,104
  • 2
  • 15
  • 26