views:

52

answers:

3

I have an XML like below :-

<SourceXML>
<Issue ID="123">
<Fields>
<Number>5</Number>
</Fields>
</Issue>
<Issue ID="125">
<Fields>
<Number>8</Number>
</Fields>
</Issue>
<Issue ID="127">
<Fields>
<Number>11</Number>
</Fields>
</Issue>
</SourceXML>

I have to get all the Issue nodes which have number as 11 or 8(where clause filter)

I tried the below but Input will be comma seperated numbers for example 8,11

  var result= from c in XElement.Load("path").Elements("Issue")
                         where c.Element("Fields").Element("Number").Value == Input
                          select c;

Basically i want the below

<Issue ID="125">
<Fields>
<Number>8</Number>
</Fields>
</Issue>
<Issue ID="127">
<Fields>
<Number>11</Number>
</Fields>
</Issue>

Also i want to write the result into a new xml file.

Please tell me how to go about it, i am dummy in LINQ

+1  A: 

You could try:

string[] values = Input.Split(new char[] { ',' });
var result= from c in XElement.Load("path").Elements("Issue")
                where values.Contains(c.Element("Fields").Element("Number").Value)
                select c;
Lazarus
thank u :-) it worked
srivatsayb
A: 

Lazarus's answer gives you what you need. To write it to a new xml file, create a new XElement and save it:

XElement answer = new XElement("Results", result);
answer.Save( fileName );
amaca
A: 

You are pretty close! Try this.

IEnumerable<XElement> results = from c in XDocument.Load("input.xml").Elements("SourceXML").Elements("Issue")
                                where c.Element("Fields").Element("Number").Value == "8" || c.Element("Fields").Element("Number").Value == "11"
                                select c;

XDocument resultXML = new XDocument();
resultXML.Add(new XElement("SourceXML"));
resultXML.Element("SourceXML").Add(results);
resultXML.Save("output.xml");
RBW_IN