views:

54

answers:

4

I am developing asp.net mobile application. I am using the XML as a database. I am using the following part of the XML file to query the data by using the LINQ to XML.

<USER-INTERFACE-DEFINITION>
      <MIMICS>
       <MIMIC ID="1" NAME="Home">
        <SECTIONS>
         <SECTION ID ="1" NAME="System Health" CONTROL-TYPE="Button">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID ="2" NAME="Network Status" CONTROL-TYPE="Button">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID ="3" NAME="ESD Status" CONTROL-TYPE="Button">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID ="4" NAME="DWPLEM" CONTROL-TYPE="Button">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID="5" NAME="Manifolds" CONTROL-TYPE="Drowpdown">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID ="6" NAME="Wells" CONTROL-TYPE="Drowpdown">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID ="7" NAME="Alarms" CONTROL-TYPE="Button">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
         <SECTION ID ="8" NAME="House Keeping" CONTROL-TYPE="Button">
          <DATAITEMS>
          </DATAITEMS>
         </SECTION>
        </SECTIONS>
       </MIMIC>
    </USER-INTERFACE-DEFINITION>

In the above XML file I want to retrive the "NAME" attribute of the SECTION node with the condition MIMIC ID="1". I dont want to modify my existing XML file. I have the server collection of node with simialr elements as the above XML file. Can you please provide me any code or link through which I can resolve the above issue ?

A: 

Perhaps something like (not LINQ, but you can use it with LINQ if you like):

var nodes = myXmlDoc.SelectNodes("/USER-INTERFACE-DEFINITION/MIMICS/MIMIC[@ID='1']/SECTION/@NAME");
foreach(var node in nodes)
{
    string name = node.Value;
    // do something with each name
}

(assuming myXmlDoc is created using .Load(yourXmlFileHer) and assuming USER-INTERFACE-DEFINITION is the root of the XML, otherwise, start with // instead)

Abel
A: 

I'll give it my best try but you might wan't to supply the relevant part of the XML to make it easier for us. Does the section have this condition or the name. At least I might get you started.

Let's start with this:

var name = xml.Descendants<SECTION>.Where(s => s.id == 1).GetFirstChild<NAME>();

Hoping I'm not way off, if that's the case I guess someone will suply a better answer.

Ok after seeing your XML I was some way off. Also when I think about it Descendants probably woudln't work.

Ingó Vals
You're trying to use names as generic type arguments... you mean `Descendants("SECTION")` etc.
Jon Skeet
Yeah just noted that in my edit, also didn't see the XML before my answer so this is some way off.
Ingó Vals
+3  A: 
var xml = XElement.Parse("your xml");
var q = from m in xml.Descendants("MIMIC")
        where (int)m.Attribute("ID") == 1
        from s in m.Descendants("SECTION")
        select (string)s.Attribute("NAME");

foreach ( var name in q ) {
    Console.WriteLine(name);
}

I have tested the above in LINQPad and it does indeed produce:

    System Health
    Network Status
    ESD Status
    DWPLEM
    Manifolds
    Wells
    Alarms
    House Keeping
Josh Einstein
+1, after XPath, probably clearest solution here :)
Abel
I actually hate LINQ to XML because the equivalent XPath is almost always cleaner. But since the OP asked for LINQ to XML I figured I'd comply. :)
Josh Einstein
Thanks for the solution. If you please tell me how to retrieve all the elements - ID ="1" NAME="System Health" CONTROL-TYPE="Button" at a time then it would help me a lot.
Shailesh Jaiswal
Instead of select (string)s.Attribute("NAME") just change it to select s. That will get you the entire SECTION node.
Josh Einstein
A: 

Assuming your XML is held in a string called xml then:

var root = XElement.Parse(xml);
var results = root
            .Descendants("MIMIC")
            .Where( e => e.Attribute("ID").Value == "1" )
            .SelectMany(e => e.Descendants("SECTION").Attributes("NAME" ) ) ;
Winston Smith