XML search for string in a node and search for another string within that node as a result
-
terça-feira, 13 de março de 2012 11:46
Hi
I want to seach some XML for a string and then when i find that string I want to check the node that string is in for another string. See example below
<Objects ObjectName="HelloObjectName" ObjectAddress="2222">
...</Objects>
How would I using C# search for string "2222" and then find out the ObjectName from within that node "HelloObjectName".
Kind regards
Rob
Todas as Respostas
-
terça-feira, 13 de março de 2012 11:56
You can use LINQ-to-XML:
XDocument doc = XDocument.Load(@"D:\Data.xml"); // The path of your XML document. var objectName = (from n in doc.Descendants("Objects") where n.Attribute("ObjectAddress").Value == "2222" select n.Attribute("ObjectName")).FirstOrDefault();
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva- Editado Marco MinervaMicrosoft Community Contributor terça-feira, 13 de março de 2012 11:57
- Sugerido como Resposta Rajasekhar.R terça-feira, 13 de março de 2012 13:04
- Marcado como Resposta Leo Liu - MSFTModerator terça-feira, 20 de março de 2012 05:25
-
terça-feira, 13 de março de 2012 12:09
Hi Marco
Excellent thanks I will give that a try
Kind regards
Rob
-
terça-feira, 13 de março de 2012 12:30
Hi Marco
You don't happen to know the correct namespace for xDocument do you because for some reason I am trying to add
Using System.Xml.Linq
but it cannot find System.Xml.Linq;
System.Xml on its own or System.Linq on its own but not System.Xml.Linq;
I am using .Net 4
Kind regards
Rob -
terça-feira, 13 de março de 2012 12:41
Sorry I am being stupid I just need to add System.Xml.Linq into my references then I could used it.
Cheers
Rob
-
terça-feira, 13 de março de 2012 12:51No problem :-)
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 08:12
Hi Marco
Thanks for your help on this, I have been away for a bit and only just come back to this.
I keep getting an error in my code 'Object reference not set to an instance of an object'. I know that the value it is trying to find for the ObjectAddress is in there but it still comes back with that error. Any ideas?
Kind regards
Rob
-
segunda-feira, 2 de abril de 2012 08:33
Perharps you need to check if the ObjectAddress attribute exists:
where n.Attribute("ObjectAddress") != null && n.Attribute("ObjectAddress").Value == "2222"Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 08:54
Thanks Marco
Its still coming back with Null unfortunately and the value for ObjectAddress definitely exists. Do I have to force the Linq to XML to read from the top of the XML file for something daft like that?
Kind regards
Rob
-
segunda-feira, 2 de abril de 2012 09:03Could you post the XML file that gives you the error?
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 09:11
Hi Marco
Unfortunately I don't think I can, any other suggestions?
Kind regards
Rob
-
segunda-feira, 2 de abril de 2012 09:15Try posting the full error message that you obtain.
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 09:35
The full error message is
'Object reference not set to an instance of an object'
The object name is definitely coming back as 'Null'. But when I just search the XML file myself I find the value.
Kind regards
Rob
-
segunda-feira, 2 de abril de 2012 09:40
Without seeing the XML input file, it is difficult to understand the problem. With the example file I have created, everything works fine, so probably the error depends on the actual structure of your file.
Can you create a small XML file to reproduce the error, that you can send me?
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 13:25
Hi Marco
I think I might be onto something now, I have stripped everything out apart from one node and then I searched for that Nodes ObjectAddress attribute value knowing it was there and I got a result back for ObjectName attribute value, but when I added another 2 Object Nodes in and searched on one of those ObjectAddress attribute values I got the following error:
'There are multiple root elements. Line 10, position 3'
Kind regards
Rob
-
segunda-feira, 2 de abril de 2012 13:27It seems that there is an error in your XML file definition, but if I don't see it, I can tell you what is the problem.
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 13:30
Actually I have solved that, that's my fault, because I stripped everything back and did not have a root node surrounding everything. I can get the values back now. I still have problem though because obviously something in my XML before I stripped it must have something in it that is causing a Null to be returned.
Kind regards
Rob
-
segunda-feira, 2 de abril de 2012 13:34I'm sorry, but without an XML file that reproduces the error, I can't help you.
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
segunda-feira, 2 de abril de 2012 13:37
No worries thanks for your help anyway.
Cheers
Rob
-
segunda-feira, 2 de abril de 2012 14:25
We found the solution, it is because of the namespace in the XML file, you need to place the namespace before your descendant node and it seems
to do the trick.
When I stripped the XML doc back and had no namespace, there was no problem, when I put it back in there was. So adding it before the
decendant node sorts out the problem.
Thanks for your help Marco.
Cheers
Rob
See below......
XNamespace ns = "http://namespace.org/NamespaceExample.xsd"
XDocument doc = XDocument.Load(@"D:\Data.xml"); // The path of your XML document. var objectName = (from n in doc.Descendants(ns + "Objects")
where n.Attribute("ObjectAddress") != null && n.Attribute("ObjectAddress").Value == "2222"
select n.Attribute("ObjectName")).FirstOrDefault();
-
segunda-feira, 2 de abril de 2012 14:32OK, so it was a namespace problem... It makes sense.
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva

