views:

43

answers:

2

I am developing asp.net mobile application. I am using XML as a database. I am using the following query for required output. In the XML file I have the collection of MIMIC nodes & inside MIMICS node I have collection of SECTION nodes. One or more more SECTION node contains one or more SECTION nodes ( nested SECTION node) In the SECTION node I have DATAITEM as follows

<MIMIC ID="3" NAME="Network Status">
                <SECTIONS>
                    <SECTION ID="1" NAME="SDA Server 1" HAS-SUBSECTIONS="TRUE">
            <DATAITEM NAME="ABC">XYZ</DATAITEM>
                        <SECTION ID="2" NAME="Top Side">
                            <DATAITEMS>
                                <DATAITEM>Not Available</DATAITEM>
                            </DATAITEMS>
                        </SECTION>
                        <SECTION ID="3" NAME="Subsea" HAS-SUBSECTIONS="TRUE">
                            <SECTION ID="4" NAME="SDA">
                                <DATAITEMS>
                                    <DATAITEM NAME="SEMA">
                                        <ATTRIBUTE NAME="TYPE" VALUE="?" APPEND-TAG-NAME-BY ="?"/>
                                        <ATTRIBUTE NAME="TagName" VALUE="?"/>
                                        <ATTRIBUTE NAME="OPCTagName"  VALUE="?"/>
                                    </DATAITEM>
                                    <DATAITEM NAME="SEMB">
                                        <ATTRIBUTE NAME="TYPE" VALUE="?" APPEND-TAG-NAME-BY ="?"/>
                                        <ATTRIBUTE NAME="TagName" VALUE="?"/>
                                        <ATTRIBUTE NAME="OPCTagName"  VALUE="?"/>
                                    </DATAITEM>
                                </DATAITEMS>
                            </SECTION>
                            <SECTION ID="5" NAME="Manifolds" HAS-SUBSECTIONS="TRUE">

For the above XML file I am using following XML query

string MIMIC_ID = "3";
string SECTION_ID = "1";



var QueryResultSet = from Mimic in FieldRoot.Element("USER-INTERFACE-DEFINITION").Element("MIMICS").Descendants("MIMIC")
                                 .Where(e => e.Attribute("ID").Value == MIMIC_ID)
                                 from DataItem in Mimic.Descendants("SECTION")
                                 .Where(e => e.Attribute("ID").Value == SECTION_ID)
                                 .Descendants("DATAITEM")
                                 select DataItem;

In the above XML query I want to access the DATAITEM node only for the SECTION node whose ID=1. But I am getting all the DATAITEM node whose SECTION ID is 2,3,4,5. I think it is because I am using the Mimic.Descendants("SECTION"). Is there any other method which can replace the Mimic.Descendants("SECTION") method so that I can access the DATAITEM node only for SECTION node whose ID=1 ? Can you provide me the code or any link through which I can resolve the above issue

A: 

If I understand your input XML correctly (as the sample above doesn't actually show DATAITEM children belonging to SECTION ID=1 (only the nested sections have DATAITEM children), then in your LINQ query instead of .Descendatns("DATAITEM") use:

.Element("DATAITEMS").Elements("DATAITEM")

Descendants will walk the whole subtree of the node you call it on and will return any element with the specified name, since your sections are nested it looks into the nested sections as well.

Vitek Karas MSFT
A: 

First in the above XML file there is need to add ID attribute to the each node of DATAITEMS & SECTION. Then use the following query

var QueryResultSet = from Mimic in FieldRoot.Element("USER-INTERFACE-DEFINITION").Element("MIMICS").Descendants("MIMIC")
                                     .Where(e => e.Attribute("ID").Value == MIMIC_ID)
                                     from DataItem in Mimic.Descendants("SECTION")
                                     .Where(e => e.Attribute("ID").Value == SECTION_ID)
                                     .Descendants("DATAITEM").Where(e =>   
                                     e.Parent.Attribute("ID").Value == SECTION_ID)
                                     select DataItem;

In the above query the following part

.Descendants("DATAITEM").Where(e => e.Parent.Attribute("ID").Value == SECTION_ID)

checks for the parent node which can be either SECTION node or DATAITEMS node. You must be careful that the node SECTION & DATAITEMS must have the simialr ID. In these two nodes, SECTION ID is acting as the primary key & DATAITEMS ID is acting as a foreign key. Thus whether DATAITEM are placed below SECTION node or below DATAITEMS node, the above query find out all the required DATAITEM node with the condition SECTION ID=1 or any SECTION ID you want to specify.

Shailesh Jaiswal