Replacing an element using DOM

S

sony.m.2007

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
 
A

Arne Vajhøj

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

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();
}
}
}
 
S

sony.m.2007

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



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>


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();
}
}

}

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
 
A

Arne Vajhøj

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>
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();
}
}

}
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top