-1

I am using C# .

I have an xml node with child nodes as follows :

<PriceID>32</PriceID>
<Store_1> 344</Store_1>
      <Store_32> 343 </Store_32>

I would like to select all nodes that start with Store

Is there a way I can do it ?

I know there is a way to select nodes with specific names ..

  XmlNodeList xnList = quote.SelectNodes("Store_1");

Does anyone know what will help me ?

CodeNinja
  • 3,188
  • 19
  • 69
  • 112

1 Answers1

3

You can use Linq2Xml

var xDoc = XDocument.Parse(xmlstring);
var stores = xDoc.Descendants()
            .Where(d => d.Name.LocalName.StartsWith("Store"))
            .ToList();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • +1 Thanks L.B ! Do you have a solution using xpath & c# ? We dont use linq at all right now.. & it might be odd to use linq only for this . – CodeNinja Jun 09 '14 at 18:12
  • 1
    @PowerCoder that kind of XML with numbered suffixes on element names is a sure sign of really poor XML design. Change it if you can. – William Walseth Jun 09 '14 at 18:54
  • @WilliamWalseth : Thats just a quick example to help people understand my problem, That is not the xml I have . Hope the downvote was not for the structure of the xml. – CodeNinja Jun 09 '14 at 20:26
  • No worries, no downvote from me. I'm just dealing with a similar issue now with a bunch of new XML developers that consider it "proper" to have every node have a unique name (by adding a _xxx number suffix). It really makes using the document difficult for the very reason you're running into. – William Walseth Jun 10 '14 at 12:00