> I need to find an element in the XML file and replace the element with
> another element.
[quoted text clipped - 61 lines]
>
> Any help to achieve above output
See code below.
Arne
PS: I think it is a very bad replacement !
===========================================
using System;
using System.Xml;
namespace E
{
public class Program
{
public static void Modify(XmlDocument doc, int val1, int val2,
int val3)
{
XmlElement elm =
(XmlElement)doc.SelectSingleNode("//details/detail[t1[1]='" + val1 + "'
and t1[2]='" + val2 + "' and t1[3]='" + val3 + "']");
elm.RemoveChild(elm.SelectSingleNode("t1[text()='" + val1 +
"']"));
elm.RemoveChild(elm.SelectSingleNode("t1[text()='" + val2 +
"']"));
elm.RemoveChild(elm.SelectSingleNode("t1[text()='" + val3 +
"']"));
XmlElement t1 = doc.CreateElement("t1");
t1.AppendChild(doc.CreateTextNode(val1 + " or " + val2 + "
or " + val3));
elm.AppendChild(t1);
}
public static void Main(string[] args)
{
string xml = @"<?xml version='1.0'?>
<details>
<detail>
<description>name</description>
<t1>10</t1>
<t1>100</t1>
<t1>1000</t1>
</detail>
<detail>
<description>name</description>
<t1>11</t1>
<t1>111</t1>
<t1>11111</t1>
</detail>
<detail>
<description>name</description>
<t1>21</t1>
<t1>221</t1>
<t1>2221</t1>
</detail>
</details>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Modify(doc, 10, 100, 1000);
doc.Save(Console.Out);
Console.ReadKey();
}
}
}
sony.m.2007@googlemail.com - 24 Mar 2008 10:57 GMT
> sony.m.2...@googlemail.com wrote:
> > I need to find an element in the XML file and replace the element with
[quoted text clipped - 127 lines]
>
> }
HI ,
The XML element is not always same in some case, if i have t1 as
elements it's working fine
But sometimes XML file is like below
<?xml version="1.0" encoding="utf-8" ?>
<details>
<detail>
<description>name</description>
<t1>10</t1>
<t1>100</t1>
<t1>1000</t1>
</detail>
<detail>
<description>name</description>
<t2>11</t2>
<t3>111</t3>
<t4>11111</t4>
</detail>
<detail>
<description>name</description>
<t5>21</t5>
<t6>221</t6>
<t5>2221</t5>
</detail>
<detail>
<description>name</description>
<t1>21</t1>
<t2>221</t2>
<t1>2221</t1>
</detail>
</details>
How to read the above XML file having different elements
Thanks,
Sony
Arne Vajhøj - 25 Mar 2008 01:58 GMT
>> sony.m.2...@googlemail.com wrote:
>>> I need to find an element in the XML file and replace the element with
[quoted text clipped - 49 lines]
>>> </detail>
>>> </details>
>> using System;
>> using System.Xml;
[quoted text clipped - 52 lines]
>>
>> }
> The XML element is not always same in some case, if i have t1 as
> elements it's working fine
[quoted text clipped - 29 lines]
> </details>
> How to read the above XML file having different elements
The code works with the XML above as well.
If you have different XML structures then you modify the code
to deal with it.
If you can describe what to select under what conditions, then you
can code it with XmlDocument and SelectSingleNode/SelectNodes.
Arne