Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Replacing an element using DOM

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sony.m.2007@googlemail.com - 24 Mar 2008 00:03 GMT
Hi,
I need to find an element in the XML file and replace the element with
another element.
I'm planning to use DOM.
Is there any tutorial available for searching an element and replace
using dom

Example xml file

<?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>
 <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>

I need to find where element t1 like this in following order <t1>10</
t1>
 <t1>100</t1>
 <t1>1000</t1>

Then I need to replace like this <t1>10 or 100 or 1000</t1>
the output like this

<?xml version="1.0" encoding="utf-8" ?>

<details>
<detail>
 <description>name</description>
 <t1>10 or 100 or 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>

Any help to achieve above output

Thanks,
Sony
Arne Vajhøj - 24 Mar 2008 01:43 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.