Seraching in XML by LINQ ?
-
Hi there. I have a XML file like this :
<NODE15>
<BOOKMARK16 NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
<BOOKMARK18 NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
<BOOKMARK19 NAME="Mozilla Support" />
<BOOKMARK20 HREF="http://www.download.com/" />
<BOOKMARK21 HREF="http://firefoxplanet.ir/" />
<BOOKMARK22 HREF="http://developer.mozilla.org/en/docs/Extensions" />
<BOOKMARK23 HREF="http://forums.mozillazine.org/viewforum.php?f19" />
<BOOKMARK24 HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
</NODE15>I want to search in it by LINQ. I used this code
X_Element = XElement.Load(FileName);
var searchNodes = from nodes in X_Element.Descendants()
where Regex.IsMatch(nodes.Attribute("HREF").Value, "Mozilla", RegexOptions.IgnoreCase)
select nodes;But there is no data in
searchNodes
What's wrong with it ? Thanks in advance -
Hi there. I have a XML file like this :
<NODE15>
<BOOKMARK16 NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
<BOOKMARK18 NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
<BOOKMARK19 NAME="Mozilla Support" />
<BOOKMARK20 HREF="http://www.download.com/" />
<BOOKMARK21 HREF="http://firefoxplanet.ir/" />
<BOOKMARK22 HREF="http://developer.mozilla.org/en/docs/Extensions" />
<BOOKMARK23 HREF="http://forums.mozillazine.org/viewforum.php?f19" />
<BOOKMARK24 HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
</NODE15>I want to search in it by LINQ. I used this code
X_Element = XElement.Load(FileName);
var searchNodes = from nodes in X_Element.Descendants()
where Regex.IsMatch(nodes.Attribute("HREF").Value, "Mozilla", RegexOptions.IgnoreCase)
select nodes;But there is no data in
searchNodes
What's wrong with it ? Thanks in advanceI assume the code fragment was incorrectly copied: 1) X_Element should be "XElement X_Element" 2) you assume the attribute exists, which in the first bookmark results in a null reference - so I don't know how you got any results without an error The code below works:
var X\_Element = XElement.Load("XMLfile1.xml"); var searchNodes = from nodes in X\_Element.Descendants() where nodes.Attribute("HREF") != null && Regex.IsMatch(nodes.Attribute("HREF").Value, "Mozilla", RegexOptions.IgnoreCase) select nodes; Console.WriteLine("There are {0} nodes", searchNodes.Count()); Console.ReadKey();
I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:
<NODE>
<BOOKMARK NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
<BOOKMARK NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
<BOOKMARK NAME="Mozilla Support" />
<BOOKMARK HREF="http://www.download.com/" />
<BOOKMARK HREF="http://firefoxplanet.ir/" />
<BOOKMARK HREF="http://developer.mozilla.org/en/docs/Extensions" />
<BOOKMARK HREF="http://forums.mozillazine.org/viewforum.php?f19" />
<BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
</NODE>'Howard
-
I assume the code fragment was incorrectly copied: 1) X_Element should be "XElement X_Element" 2) you assume the attribute exists, which in the first bookmark results in a null reference - so I don't know how you got any results without an error The code below works:
var X\_Element = XElement.Load("XMLfile1.xml"); var searchNodes = from nodes in X\_Element.Descendants() where nodes.Attribute("HREF") != null && Regex.IsMatch(nodes.Attribute("HREF").Value, "Mozilla", RegexOptions.IgnoreCase) select nodes; Console.WriteLine("There are {0} nodes", searchNodes.Count()); Console.ReadKey();
I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:
<NODE>
<BOOKMARK NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
<BOOKMARK NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
<BOOKMARK NAME="Mozilla Support" />
<BOOKMARK HREF="http://www.download.com/" />
<BOOKMARK HREF="http://firefoxplanet.ir/" />
<BOOKMARK HREF="http://developer.mozilla.org/en/docs/Extensions" />
<BOOKMARK HREF="http://forums.mozillazine.org/viewforum.php?f19" />
<BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
</NODE>'Howard
Thank you very much Howard. Is this a good way for searching in big XML file ?
Howard Richards wrote:
I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:
But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute):confused:
-
Thank you very much Howard. Is this a good way for searching in big XML file ?
Howard Richards wrote:
I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:
But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute):confused:
Mohammad Dayyan wrote:
But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute)
Lets say the XML is
<NODE>
<BOOKMARK ID="1" NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
<BOOKMARK ID="2" NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
<BOOKMARK ID="3" NAME="Mozilla Support" />
<BOOKMARK ID="4" HREF="http://www.download.com/" />
<BOOKMARK ID="5" HREF="http://firefoxplanet.ir/" />
<BOOKMARK ID="6" HREF="http://developer.mozilla.org/en/docs/Extensions" /><BOOKMARK ID="7" HREF="http://forums.mozillazine.org/viewforum.php?f19" /> <BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
</NODE>If you wanted to pick a specific node the easiest way is to specify ID=3 in the where clause. However, if you don't want the ID and just want the third element in the sequence:
var X_Element = XElement.Load("XMLFile1.xml");
// select only bookmark descendants var items = from bookmark in X\_Element.Descendants("BOOKMARK") select bookmark; // get third item by skipping first two var third = items.Skip(2).FirstOrDefault(); if (third != null) { Console.WriteLine("Third item is {0}", third.Attribute("NAME").Value); } Console.ReadKey();
This will throw an exception if there are less than three elements in then sequence so you should check items.Count() before trying to get the third.
Mohammad Dayyan wrote:
Is this a good way for searching in big XML file ?
Define big! LINQ to XML using .Load will load the whole document into memory. For small applications this is usually okay. However there is a way of streaming very large XML documents in LINQ. Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML - he talks about streaming in the second half but you should find the whole thing really useful (pick the fourth video, LINQ to XML).
'Howard
-
Mohammad Dayyan wrote:
But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute)
Lets say the XML is
<NODE>
<BOOKMARK ID="1" NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
<BOOKMARK ID="2" NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
<BOOKMARK ID="3" NAME="Mozilla Support" />
<BOOKMARK ID="4" HREF="http://www.download.com/" />
<BOOKMARK ID="5" HREF="http://firefoxplanet.ir/" />
<BOOKMARK ID="6" HREF="http://developer.mozilla.org/en/docs/Extensions" /><BOOKMARK ID="7" HREF="http://forums.mozillazine.org/viewforum.php?f19" /> <BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
</NODE>If you wanted to pick a specific node the easiest way is to specify ID=3 in the where clause. However, if you don't want the ID and just want the third element in the sequence:
var X_Element = XElement.Load("XMLFile1.xml");
// select only bookmark descendants var items = from bookmark in X\_Element.Descendants("BOOKMARK") select bookmark; // get third item by skipping first two var third = items.Skip(2).FirstOrDefault(); if (third != null) { Console.WriteLine("Third item is {0}", third.Attribute("NAME").Value); } Console.ReadKey();
This will throw an exception if there are less than three elements in then sequence so you should check items.Count() before trying to get the third.
Mohammad Dayyan wrote:
Is this a good way for searching in big XML file ?
Define big! LINQ to XML using .Load will load the whole document into memory. For small applications this is usually okay. However there is a way of streaming very large XML documents in LINQ. Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML - he talks about streaming in the second half but you should find the whole thing really useful (pick the fourth video, LINQ to XML).
'Howard
Howard Richards wrote:
Lets say the XML is
Oh , Thank you. Actually I didn't know that. :-O
Howard Richards wrote:
Define big!
For example I have a XML file that there are about 1000 nodes (like above XML) in it.
Howard Richards wrote:
Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML
Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)
-
Howard Richards wrote:
Lets say the XML is
Oh , Thank you. Actually I didn't know that. :-O
Howard Richards wrote:
Define big!
For example I have a XML file that there are about 1000 nodes (like above XML) in it.
Howard Richards wrote:
Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML
Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)
I personally would not class a 1000 nodes as big - almost all current PCs able to run .NET would be able to load a file that size in memory with ease. Mike Taulty's example was a 100MB XML file which could be several million lines of XML - something like that would be too large to load into memory in one go, so streaming is the only way to process it.
'Howard
-
I personally would not class a 1000 nodes as big - almost all current PCs able to run .NET would be able to load a file that size in memory with ease. Mike Taulty's example was a 100MB XML file which could be several million lines of XML - something like that would be too large to load into memory in one go, so streaming is the only way to process it.
'Howard
Wow. it's not big, it's huge. Thank you
-
Howard Richards wrote:
Lets say the XML is
Oh , Thank you. Actually I didn't know that. :-O
Howard Richards wrote:
Define big!
For example I have a XML file that there are about 1000 nodes (like above XML) in it.
Howard Richards wrote:
Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML
Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)
Mohammad Dayyan wrote:
Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)
Ah - I could not see a way to download that video either. Try these smaller ones - I think they can [^] - these you can download and watch offline.
'Howard
-
Mohammad Dayyan wrote:
Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)
Ah - I could not see a way to download that video either. Try these smaller ones - I think they can [^] - these you can download and watch offline.
'Howard
Thanks again my friend.