<?xml version="1.0" encoding="utf-8"?>
<Data>
<Table Id="Queries">
<!--
Queries are executed in the order they appear in this file.
By default 'Start over' will be executed between each query. If you want to suppress this,
add a paremeter with the name 'ExecuteStartOver' and the value 'false'.
-->
<Row>
<Parameter Name="Id">1</Parameter>
<Parameter Name="Name">Peter</Parameter>
XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);
// get a list of nodes - in this case, I'm selecting all <Row> nodes under
// the <Table> node - change to suit your needs
XmlNodeList aNodes = doc.SelectNodes("/Data/Table/Row");
int j = 0;
foreach (XmlNode aNode in aNodes)
{
// check if it is first entry or note ...
if (j != 0)
{
foreach (XmlNode bNode in aNode.ChildNodes)
{
if (bNode.Attributes[0].Value == "Id")
{
bNode.InnerText = "Somthing Dynamic";
}
if (bNode.Attributes[0].Value == "Name")
{
bNode.InnerText = "Somthing Dynamic";
}
Hi Subhranshu,
Try:
foreach (XmlNode aNode in aNodes)
{
// check if it is first entry or note ...
if (j != 0)
{
foreach (XmlNode bNode in aNode.ChildNodes)
{
//Create a comment.
XmlComment newComment;
newComment = doc.CreateComment("Something");
if (bNode.Attributes[0].Value == "Id")
{
bNode.InnerText = "Somthing Dynamic";
//Place comment before current node
doc.InsertBefore(newComment, bNode);
}
if (bNode.Attributes[0].Value == "Name")
{
bNode.InnerText = "Somthing Dynamic";
}
}
j++;
}
}
i am get below error by using ur code
"The reference node is not a child of this node."
i think insertbefore fuction take two parameter (XmlNode newnode, XMLNode refnode) nut here we passing comment may that'll be the reason behind it
i have correct the error ....
Thanks Oli Cartwright
foreach (XmlNode aNode in aNodes)
{
// check if it is first entry or note ...
if (j != 0)
{
foreach (XmlNode bNode in aNode.ChildNodes)
{
//Create a comment.
XmlComment newComment;
newComment = doc.CreateComment("Something");
if (bNode.Attributes[0].Value == "Id")
{
bNode.InnerText = "Somthing Dynamic";
//Place comment before current node
//doc.InsertBefore(newComment, bNode);
Member
4 Points
32 Posts
How to insert a commented line in each child node
Aug 31, 2013 09:03 AM|Subhranshu|LINK
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Table Id="Queries">
<!--
Queries are executed in the order they appear in this file.
By default 'Start over' will be executed between each query. If you want to suppress this,
add a paremeter with the name 'ExecuteStartOver' and the value 'false'.
-->
<Row>
<Parameter Name="Id">1</Parameter>
<Parameter Name="Name">Peter</Parameter>
</Row>
<Row>
<Parameter Name="Id">2</Parameter>
<Parameter Name="Name">Ronaldo</Parameter>
</Row>
</Table>
</Data>
Suppose i have one xml file like this
i want to add one commented line under each node
means
i want this
<Row>
<!-- Something -->
<Parameter Name="Id">1</Parameter>
<Parameter Name="Name">Peter</Parameter>
</Row>
<Row>
<!-- Something -->
<Parameter Name="Id">2</Parameter>
<Parameter Name="Name">Ronaldo</Parameter>
</Row>
My C# code
XmlDocument doc = new XmlDocument();
doc.Load(xmlfile);
// get a list of nodes - in this case, I'm selecting all <Row> nodes under
// the <Table> node - change to suit your needs
XmlNodeList aNodes = doc.SelectNodes("/Data/Table/Row");
int j = 0;
foreach (XmlNode aNode in aNodes)
{
// check if it is first entry or note ...
if (j != 0)
{
foreach (XmlNode bNode in aNode.ChildNodes)
{
if (bNode.Attributes[0].Value == "Id")
{
bNode.InnerText = "Somthing Dynamic";
}
if (bNode.Attributes[0].Value == "Name")
{
bNode.InnerText = "Somthing Dynamic";
}
}
j++
}
How can i do that through C#
please help me on this
Thanks in advance
Member
290 Points
71 Posts
Re: How to insert a commented line in each child node
Aug 31, 2013 11:08 AM|Oli Cartwright|LINK
Hi Subhranshu,
Try:
Member
4 Points
32 Posts
Re: How to insert a commented line in each child node
Aug 31, 2013 03:59 PM|Subhranshu|LINK
i am get below error by using ur code
"The reference node is not a child of this node."
i think insertbefore fuction take two parameter (XmlNode newnode, XMLNode refnode) nut here we passing comment may that'll be the reason behind it
Member
4 Points
32 Posts
Re: How to insert a commented line in each child node
Aug 31, 2013 04:55 PM|Subhranshu|LINK