using query in XML file  
Author Message
Jagjot Singh





PostPosted: XML and the .NET Framework, using query in XML file Top

Hi Folks,


< xml version="1.0" standalone="yes" >
<NewDataSet>
<XML_TEST_NAME>
<PRODUCT_NAME>ALx</PRODUCT_NAME>
<TEST_NAME>NBQ SEx</TEST_NAME>
</XML_TEST_NAME>
<XML_TEST_NAME>
<PRODUCT_NAME>ALxxA</PRODUCT_NAME>
<TEST_NAME>NBQ SExxxA</TEST_NAME>
</XML_TEST_NAME>
</NewDataSet>




.NET Development6  
 
 
Greg Beech





PostPosted: XML and the .NET Framework, using query in XML file Top

Not using SQL syntax but you can use an XPath query, e.g.

string myvalue = "ALx";
XmlDocument doc = new XmlDocument();
doc.Load("myfile.xml");
XmlNodeList nodes = doc.SelectNodes("NewDataSet/XML_TEST_NAME[PRODUCT_NAME='" + myvalue + "']/TEST_NAME");
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.InnerText); // prints NBQ SEx
}

The square brackets are used to define a predicate, selecting only XML_TEST_NAME nodes where PRODUCT_NAME is the desired value, and then gets the TEST_NAME node under that.



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, using query in XML file Top

Do read and study XPath -- it is to XML what SQL is for relational data.

To answer your question, one can generally evaluate an XPath expression as the following:

//*[name() = someParamString]

then get the string value of each node selected and use it to populate the whatever control.

Cheers,
Dimitre Novatchev


 
 
Greg Beech





PostPosted: XML and the .NET Framework, using query in XML file Top

The // operator is woefully slow though, particularly with large documents, as it will cause the entire node tree to be traversed. If you know the structure of your data in advance then prefer to specify the full path to the node which allows much quicker access to it.

Also the expression you put would match the node name rather than the text contents of the node which is required here.



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, using query in XML file Top

The // operator is woefully slow though, particularly with large documents, as it will cause the entire node tree to be traversed. If you know the structure of your data in advance then prefer to specify the full path to the node which allows much quicker access to it.

Also the expression you put would match the node name rather than the text contents of the node which is required here.

Very true. My answer matched the generality of the OP. He was asking for "field" and there was no element named "field" in the provided XML.

Also, there is no guarantee that *in general* an XPath expression will select a single element named "someFieldName" -- this is why my advise was:

"then get the string value of each node selected and use it to populate the whatever control".

It is a common problem in such forums that people are usually asking vague and not well-defined questions. To answer one has either to match their vagueness in the reply or to be psychic and guess exactly what the specific case was.

Cheers,
Dimitre Novatchev


 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, using query in XML file Top

Hi Folks,


< xml version="1.0" standalone="yes" >
<NewDataSet>
<XML_TEST_NAME>
<PRODUCT_NAME>ALx</PRODUCT_NAME>
<TEST_NAME>NBQ SEx</TEST_NAME>
</XML_TEST_NAME>
<XML_TEST_NAME>
<PRODUCT_NAME>ALxxA</PRODUCT_NAME>
<TEST_NAME>NBQ SExxxA</TEST_NAME>
</XML_TEST_NAME>
</NewDataSet>

The exact answer is No, because there are no elements named "test_name" and "Product_name" in the provided xml document.

In case you meant "TEST_NAME" and "PRODUCT_NAME", then one XPath expression that would select the corresponding text nodes is:

/*/*[PRODUCT_NAME = 'someParamString']/TEST_NAME/text()

Cheers,
Dimitre Novatchev


 
 
Jagjot Singh





PostPosted: XML and the .NET Framework, using query in XML file Top

Hi,

My apologies for not being clear .(not going to happen again, take it as positive remark)

yes Dimitre you are right its  "TEST_NAME" and "PRODUCT_NAME used by select statement.

you code worked excellent and fast enough to satisfy my manager and myself.

Thanks alot guys. (if you can guide me to read any on line article or tutorial will be ++ for me.)

Regards



 
 
Jagjot Singh





PostPosted: XML and the .NET Framework, using query in XML file Top

Hi,

i am againg stuck in query stuff do you know how to use and condition in query. i am trying the following.

Dim myXPathNodeIterator2 As Me Me.Test_Cmb.SelectedValue & "']")

Thanks in advance

Regards



 
 
Greg Beech





PostPosted: XML and the .NET Framework, using query in XML file Top

It's just the word "and". Assuming you want to select where both attributes p_name and t_name match:

Dim myXPathNodeIterator2 As Me Me.Test_Cmb.SelectedValue & "']")