Sample
The sample code finds the attribute names of the elements whose children elements'values match the value entered in a texbox
Now look at my code and XMLfile and you'll see that they're identical to the above link. The problem is that my code only prints the first group that matches the text box value.
XML
<?xml version="1.0" encoding="utf-8"?>
<groups>
<group name="a">
<ip>10.3.4</ip>
<ip>10.1.4</ip>
</group>
<group name="b">
<ip>10.2.1</ip>
<ip>10.3.4</ip>
<ip>10.55.55</ip>
</group>
</groups>
Code
XElement root = XElement.Load("c:\etc);
IEnumerable<XElement> tests =
from el in root.Elements("group")
where (string)el.Element("ip") == textBox1.Text
select el;
foreach (XElement el in tests)
Console.WriteLine((string)el.Attribute("name"));
The problem is in the where clause. Because if i comment it, the system will print both group names but when the where clause is active, it's always only returning 1 group. might as well be using FirstOrDefault() -_-
var tests =
from el in root.Elements("group")
where el.Elements("ip").Any(o => o.Value == textBox1.Text)
select el;
Try this code:
XElement root = XElement.Load(#"your path to a file");
//set text box to some default value to test if function will work
textBox1.Text = "10.1.4";
//here I used etension method, commented is alternative version, for better understanding
IEnumerable<XElement> tests = root.Elements("group").Where(gr => gr.Elements("ip").Any(ip => ip.Value == textBox1.Text));
//IEnumerable<XElement> tests = root.Elements("group").Where(gr => gr.Elements("ip").Where(ip => ip.Value == textBox1.Text).Count() > 0);
foreach (XElement el in tests)
//Console.WriteLine((string)el.Attribute("name"));
MessageBox.Show((string)el.Attribute("name"));
Your code didn't work, as you compared single element against the text of a text box. What you want is to check if any among the ip elements is equal to specified text.
In both groups you have the same ip
<ip> 10.3.4 </ ip>
In this case it will even bring both groups together.
I'd advise doing one more condition by checking the 'name'.
var xml = XDocument.Parse(xmlString);
// a, b
string[] matchingNames = xml.Root.Elements("group")
.Where(g => g.Elements("ip").Any(e => e.Value == textBoxText))
.Select(g => g.Attribute("name").Value).ToArray();
Select the attribute name from groups where any of the child ip elements contain the text box text.
Related
I have some projects in an XML file. eg. Multiple projects like the one below in the same file . I want to search all project entries where FluidTypes matches a particular string .
<?xml version="1.0" encoding="utf-8"?>
<data>
<Project ID="P-2014-000037">
<Name>FDP001_Bakken</Name>
<Manager>shell</Manager>
<Area>NAM</Area>
<Field>Bakken</Field>
<Type>Time and Material External</Type>
<Country>USA</Country>
<Value>3.5</Value>
<Geomarket>NAM</Geomarket>
<FormationTypes>Carbonate</FormationTypes>
<FormationTypes>Coal</FormationTypes>
<FormationTypes>Fractures</FormationTypes>
<FormationTypes>Sandstone</FormationTypes>
<FluidTypes>Gas Cond</FluidTypes>
<FluidTypes>Heavy Oil</FluidTypes>
<DriveMechanisms>Compaction</DriveMechanisms>
<DriveMechanisms>Aquifer</DriveMechanisms>
<EORProcesses>CO2</EORProcesses>
<EORProcesses>CSS</EORProcesses>
</Project>
</data>
I am using the follwing code to search for Geomarket matches :
IEnumerable<XElement> values1 = from el1 in root.Elements("Project").
Where(r => regEx1.IsMatch(r.Element("Geomarket").Value))
select el1;
when I use same for Fluid type (which has multiple elements ):
IEnumerable<XElement> values1 = from el1 in root.Elements("Project").
Where(r => regEx1.IsMatch(r.Element("FluidTypes").Value))
select el1;
It only checks for a match with the first element with name Fluid Types and not ALL fluid types elements . As a result only Gas Cond matches this project but Heavy Oil does not.
How to make a query across all Fluid types search ?
Use a Where clause with a nested search:
var projects = root
.Elements("Project")
.Where(el => el.Elements("FluidTypes").Where(el2 => regEx1.IsMatch(el2.Value)).Any());
This returns all elements named "Project" with at least one nested element named "FluidTypes" whose Value matches your regular expression.
Or, use a nested Any():
var projects = root
.Elements("Project")
.Where(el => el.Elements("FluidTypes").Any(el2 => regEx1.IsMatch(el2.Value)));
Try
IEnumerable<XElement> values1 = from el1 in root.Elements("Project").Elements("FluidTypes")
.Where(r => regEx1.IsMatch(r.Value))
Select el1;
Still a Linq newbie here, and now having issues with the WHERE clause. I'm trying to return anything found in the printer tags, but only from below the element list type="lff".
If I try to output the descendant elements with no WHERE clause, I get everything (from both <list> elements). When I try to add various versions of a WHERE clause, I get nothing back. I'm obviously not putting the WHERE condition in correctly.
(I need to get the element object, so I can check the NAME and the VALUE. In my example below, I am only outputting the VALUE for now).
Can you advise?
Here is the XML:
<?xml version="1.0"?>
<printerlist>
<list type="aff">
<printserver>print-server1</printserver>
<printserver>print-server2</printserver>
<printserver>print-server3</printserver>
<additionalprinters>
<printer>
<fullname>\\servera\bbb</fullname>
</printer>
</additionalprinters>
</list>
<list type="lff">
<printserver>print-sever4</printserver>
<additionalprinters>
<printer>
<fullname>\\serverb\bbb</fullname>
</printer>
<printer>
<fullname>\\serverc\aaa</fullname>
</printer>
</additionalprinters>
</list>
</printerlist>
And here is the code to try and get the list:
var qq = from c in xml.Descendants("additionalprinters").Descendants("printer")
//where (string) c.Parent.Attribute("type") == "lff"
//Uncommenting the above line means that nothing is returned.
select c;
foreach (XElement q in qq)
{
Console.WriteLine("Test Output: {0}", q.Value );
}
Output is:
Test Output: \\servera\bbb
Test Output: \\serverb\bbb
Test Output: \\serverc\aaa
I am only looking for the final two outputs to be returned, in this particular case.
The parent of printer is additionalprinters and it doesn't have type property, you need to use .Parent twice to get list element.
from c in xml.Descendants("additionalprinters").Descendants("printer")
where (string) c.Parent.Parent.Attribute("type") == "lff"
select c
Or you can also do the following
xml.Descendants("list")
.Where(c => (string) c.Attribute("type") == "lff")
.SelectMany(x => x.Element("additionalprinters").Descendants("printer"))
You can also use XPath selector from System.Xml.XPath namespace for this purpose:
var doc = XDocument.Parse(xml);
var printers = doc.XPathSelectElements("//list[#type='lff']/additionalprinters/printer");
Ihave a large xml file and I want to get child element value by giving the parent child element value, I am new in xml file please any help here is my xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<masterController> <uuid>XXXXXXXXXXXXXXXXXXXXXXXXX</uuid>
<channels>
<channel>
<nodeGroups>
<nodeGroup>
<analogNode>
<typeCode>8</typeCode>
<id>1</id>
<sdos>
<sdo>
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<datafield xmlns:xsi="http://www.XXXXX.XXXX/XXXXX/XMLSchema-instance"
xsi:type="intField">
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<offset>2</offset>
<size>1</size>
<readonly>true</readonly>
<isMappedToPdo>false</isMappedToPdo>
<ownerNodeSerial>12102904</ownerNodeSerial>
<ownerSdoIndex>3</ownerSdoIndex>
<data xsi:type="intData">
<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">2</value>
<unit></unit>
<min>1</min>
<max>15</max>
</data>
<intValue>2</intValue>
</datafield>
<index>3</index>
<totalbytes>3</totalbytes>
</sdo>
<sdo>
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<datafield xmlns:xsi="http://www.XXXXX.XXXX/XXXXX/XMLSchema-instance"
xsi:type="intField">
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<offset>2</offset>
<size>1</size>
<readonly>true</readonly>
<isMappedToPdo>false</isMappedToPdo>
<ownerNodeSerial>12102905</ownerNodeSerial>
<ownerSdoIndex>4</ownerSdoIndex>
<data xsi:type="intData">
<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">16</value>
<unit></unit>
<min>1</min>
<max>15</max>
</data>
<intValue>2</intValue>
</datafield>
<index>3</index>
<totalbytes>3</totalbytes>
</sdo>
</sdos>
</analogNode>
</nodeGroup>
</nodeGroups>
</channel> </channels> </masterController>
I' am trying this but am not geting anything:
XElement root = XElement.Load(Server.MapPath("sample.xml"));
IEnumerable<XElement> masterco = from el in root.Elements("sdo") where (from add in el.Elements("datafield")
where
(string)add.Element("ownerNodeSerial") == TextBox1.Text &&
(string)add.Element("ownerSdoIndex") == TextBox1.Text
select add)
.Any()
select el;
foreach (XElement el in masterco)
{
TextBox3.Text = (string)el.Element("value");
}
I want to get this:
<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">16</value>
and be able to update it.
There is one major error in your query:
You are using Elements on root, but you are looking for the tag sdo which is not a direct child of the root tag. You have to use Descendants instead.
Additionally, I think you want to have an OR instead of an AND regarding the text of TextBox1.
Fix it:
var masterco = from el in root.Descendants("sdo")
where (from add in el.Elements("datafield")
where
(string)add.Element("ownerNodeSerial") == TextBox1.Text ||
(string)add.Element("ownerSdoIndex") == TextBox1.Text
select add).Any()
select el;
To actually get the value you want, you should use a different query. There is really no need to select the sdo tag at all.
var value = root.Descendants("datafield")
.Where(x => (string)x.Element("ownerNodeSerial") == TextBox1.Text ||
(string)x.Element("ownerSdoIndex") == TextBox1.Text)
.Select(x => (string)x.Element("data").Element("value"))
.Single();
TextBox3.Text = value;
You can see that I am assuming that in the whole XML document only one matching datafield/data/value entry exists. I derive that information from the way you update your textbox. This would make no sense if there would be multiple tags - the values would overwrite each other in the text box.
I have the following XDocument called XDoc:
<?xml version="1.0" encoding="utf-8"?>
<DatabaseList>
<Database DatabaseName="c2501_data">
<Plugin PluginName="FooPlugin" LastRun="1/21/2013 3:22:08 PM" />
<Plugin PluginName="SpecialPlugin" LastRun="2013-01-21T15:22:09.3791103-05:00" />
<Plugin PluginName="BarPlugin" LastRun="2013-01-21T15:23:13.0964814-05:00" />
</Database>
</DatabaseList>
I'm writing a program that searches to see when the last time a plugin was run on a database, if at all. I use the following two pieces of code to figure out if an entry exists for a plugin on a database:
var h = (from el in XDoc.Root.Elements("Database")
where el.Element("Plugin").Attribute("PluginName").Value=="FooPlugin"
&& el.Attribute("DatabaseName").Value=="c2501_data"
select el.Element("Plugin"));
var e = (from el in XDoc.Root.Elements("Database")
where el.Element("Plugin").Attribute("PluginName").Value=="BarPlugin"
&& el.Attribute("DatabaseName").Value == "c2501_data"
select el.Element("Plugin"));
if ((from el in XDoc.Root.Elements("Database")
where el.Element("Plugin").Attribute("PluginName").Value == "BarPlugin"
&& el.Attribute("DatabaseName").Value == "c2501_data"
select el.Element("Plugin")).Count() == 0)
{
XElement SpecialPlugin = new XElement("Plugin",
new XAttribute("PluginName", "BarPlugin"),
new XAttribute("LastRun", DateTime.Now));
var CurNode = from node in XDoc.Root.Elements("Database")
where (string)node.Attribute("DatabaseName").Value == "c2501_data"
select node;
foreach (var node in CurNode)
node.Add(SpecialPlugin);
XDoc.Save(RuntimesPath);
//XDoc.Root.Elements("Database").Attribute("DatabaseName").
}
The problem that I'm having is that even though there is clearly an entry for BarPlugin, the count will always return 0 and e will always be unable to create an enumberable. Can anyone explain to me why this might be? FooPlugin always works correctly and returns the Plugin information for h.
Thanks for any help.
You're selecting a Database element where it contains a child element called Plugin with a given name. Since you have only one Database element, you're getting the same outer element each time. You then take that database element and return the first Plugin child, which will always be Foo, in this case. You need to find the appropriate Database element and then query through each of the child elements so you can return them:
public static XElement GetPlugin(XDocument XDoc, string databaseName, string pluginName)
{
var h = from database in XDoc.Root.Elements("Database")
where database.Attribute("DatabaseName").Value == databaseName
from plugin in database.Elements("Plugin")
where plugin.Attribute("PluginName").Value == pluginName
select plugin;
return h.FirstOrDefault();
}
Or, if you prefer, in method syntax:
var q = XDoc.Root.Elements("Database")
.Where(db => db.Attribute("DatabaseName").Value == databaseName)
.SelectMany(db => db.Elements("Plugin"))
.Where(plugin => plugin.Attribute("PluginName").Value == pluginName);
return q.FirstOrDefault();
Try this:
var db = XDoc.Root.Elements("Database");
var z = (from el in db.Elements("Plugin")
where el.Attribute("PluginName").Value == "BarPlugin"
&& el.Parent.Attribute("DatabaseName").Value == "c2501_data"
select el).FirstOrDefault();
if(z != null)
.....
I'm using Elements() method, to get all child elements and Parent property to look for the parent element "DatabaseName".
Problem in your code is that your el.Element() is searching only for the first element, thus it can find only "FooPlugin", which is on the first position in the xml.
From MSDN doc Element():
Gets the first (in document order) child element with the specified XName.
Trying to use
exportDoc.Root.Elements("string").Where(node => !(node.Element("product").HasElements) || node.Element("product").Element("type").Value != product).Remove();
to remove the nodes in my XML document where the product string I'm searching for doesn't occur. Here is a sample of my XML structure:
<root>
<string id = "Hithere">
<product>
<type>orange</type>
<type>yellow</type>
<type>green</type>
<product>
<element2/>
<element3/>
</string>
<string id ="...">
...
...
</root>
So I need to look under the product element of each string element AND at each of the type elements therein to see if the value of string product (input to the method where this is contained) occurs. At present, it looks like my code only removes the node if the product string I'm searching for matches the value of just the first type element.
The whole point is to remove all string nodes from this xdoc that don't have the product I'm looking for listed under their product element.
You need to change your search condition slightly:
var nodesToRemove = xDoc.Root
.Elements("string")
.Where(node =>
!(node.Element("product").HasElements) ||
node.Element("product").Elements("type").All(x => x.Value != product))
.ToList();
This should match elements which all string:product:types differ from product value (or in other words - if at least one <type> will match your product, it won't be marked for removal).
You can't Remove() while you're still enumerating (deferred execution).
You need something more like:
// untested
var toRemove = exportDoc.Root.Elements("string")
.Where(node => !(node.Element("product").HasElements) ||
node.Element("product").Element("type").Value != product).ToList();
toRemove.Remove();