Denis Kralj
Denis Kralj

Reputation: 633

Getting nodes from a parent with a specific name atribute

I have the following XML document:

<database name="pressPlay">
    <table name="users">
      <column name="userID"       type="INT"            constraints="tableID_c" />
      <column name="username"     type="VARCHAR(50)"    constraints="user_c" />
      <column name="password"     type="VARCHAR(50)"    constraints="not_null_c" />
    </table>
    <table name="song">
      <column name="songID"       type="INT"            constraints="tableID_c" />
      <column name="albumID"      type="INT"            constraints="albumFK_c" />
      <column name="artistID"     type="INT"            constraints="artistFK_c" />
      <column name="songName"     type="VARCHAR(50)"    constraints="not_null_c" />
      <column name="songDuration" type="VARCHAR(5)"     constraints="not_null_c" />
      <column name="link"         type="VARCHAR(100)" />
    </table>
    <table name="album">
      <column name="albumID"      type="INT"            constraints="tableID_c" />
      <column name="albumName"    type="VARCHAR(50)"    constraints="not_null_c" />
      <column name="albumGener"   type="VARCHAR(50)"    constraints="not_null_c" />
    </table>
    <table name="artist">
      <column name="artistID"     type="INT"            constraints="tableID_c" />
      <column name="artistName"   type="VARCHAR(50)"    constraints="not_null_c" />
    </table>

    <constraints>
      <constraint name="tableID_c"    type="unique"     content="not_null" increment="auto"/>
      <constraint name="user_c"       type="unique"     content="not_null"/>
      <constraint name="not_null_c"                     content="not_null"/>
      <constraint name="albumFK_c"    type="forign_key" content="not_null" columns="album.albumID" />
      <constraint name="artistFK_c"   type="forign_key" content="not_null" columns="artist.artistID" />
    </constraints>    
</database>

now what i need is to get attributes from nodes that share a parent with a specific name attribute.

I tried to useXmlNodeList but that gives me all the nodes named column regardless of what the parents name is, i.e:

i need the name attributes from column nodes, with the parent name being users.

anyone know how to do this? i've been busting my head for a while now..

Upvotes: 2

Views: 133

Answers (3)

dommer
dommer

Reputation: 19820

Something like this in Linq?

IEnumerable<string> names =
    from x in XDocument.Load(@"project.xml").Descendants("column")
    where x.Parent.Attribute("name").Value.Equals("users", StringComparison.Ordinal)
    select x.Attribute("name").Value;

Upvotes: 3

Royi Namir
Royi Namir

Reputation: 148704

something Like that :

XmlDocument inventory = new XmlDocument();
inventory.Load("inventory.xml");

XmlNodeList elements = inventory.SelectNodes("/XMLProject/table[@name='users']/column");
foreach (XmlElement element in elements)
{
    ...
}

this will give you :

<column name="userID"       type="INT"            constraints="tableID_c" />
<column name="username"     type="VARCHAR(50)"    constraints="user_c" />
<column name="password"     type="VARCHAR(50)"    constraints="not_null_c" />

Now you can walk through each and take its attribute.

Upvotes: 3

tbddeveloper
tbddeveloper

Reputation: 2447

http://msdn.microsoft.com/en-us/library/bb387098.aspx

I would actually use LinqToXML in this case;

XDocument document = XDocument.Load("mydocument")

var table = (from n in document.Descendants("table")
             where n.Attribute("name").Value == "users"
             select n).Single( );

var columns = from c in table.Descendants("column")
               select c.Attribute("name").Value;

That's a rough version, and I am certain there's a way to collapse them down into a single query. I find LinqToXml much easier to work with.

Upvotes: 2

Related Questions