Remove a XMLnode - Pls help
-
Hello, Below is my XML document. FP108MR 0 Z9999 I need to remove only node so that my xml document remains as below. FP108MR 0 Z9999 I used the following code to convert Dataset to a XMLdocument. Dim dtCounter As New DataSet("GetCountsResult") oDatabase.Connect() oDatabase.PopulateDataSet("Count", "Get_Current_Counter", True, dtCounter) Dim sw As New StringWriter dtCounter.WriteXml(sw, XmlWriteMode.IgnoreSchema) Dim xmldoc As New XmlDocument xmldoc.LoadXml(sw.ToString()) Please help me in getting this. Thanks.
-
Hello, Below is my XML document. FP108MR 0 Z9999 I need to remove only node so that my xml document remains as below. FP108MR 0 Z9999 I used the following code to convert Dataset to a XMLdocument. Dim dtCounter As New DataSet("GetCountsResult") oDatabase.Connect() oDatabase.PopulateDataSet("Count", "Get_Current_Counter", True, dtCounter) Dim sw As New StringWriter dtCounter.WriteXml(sw, XmlWriteMode.IgnoreSchema) Dim xmldoc As New XmlDocument xmldoc.LoadXml(sw.ToString()) Please help me in getting this. Thanks.
The
ReplaceChild
method ofXmlNode
class explained here http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.replacechild.aspx[^] can be used for replacing the desired nodes as shown below:void Main()
{
string xmlCounters =
@"<GetCountersResult>
<GetCountsResult xmlns="""">
<Count>
<ID>FP108MR</ID>
<Value>0</Value>
<Part>Z9999</Part>
</Count>
</GetCountsResult>
<GetCountsResult xmlns="""">
<Count>
<ID>FP108MR No2</ID>
<Value>0 No2</Value>
<Part>Z9999 No2</Part>
</Count>
</GetCountsResult>
</GetCountersResult>";XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlCounters); var countNodes = xmlDoc.GetElementsByTagName("Count"); //foreach throws, the element list has changed, error for (int i=0; i < countNodes.Count; i++) { xmlDoc.DocumentElement.ReplaceChild(countNodes\[i\], countNodes\[i\].ParentNode); } string fileName = @"F:\\ModifiedXml.xml"; xmlDoc.Save(fileName);
}
//The contents of file
// <GetCountersResult>
// <Count>
// <ID>FP108MR</ID>
// <Value>0</Value>
// <Part>Z9999</Part>
// </Count>
// <Count>
// <ID>FP108MR No2</ID>
// <Value>0 No2</Value>
// <Part>Z9999 No2</Part>
// </Count>
// </GetCountersResult> -
The
ReplaceChild
method ofXmlNode
class explained here http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.replacechild.aspx[^] can be used for replacing the desired nodes as shown below:void Main()
{
string xmlCounters =
@"<GetCountersResult>
<GetCountsResult xmlns="""">
<Count>
<ID>FP108MR</ID>
<Value>0</Value>
<Part>Z9999</Part>
</Count>
</GetCountsResult>
<GetCountsResult xmlns="""">
<Count>
<ID>FP108MR No2</ID>
<Value>0 No2</Value>
<Part>Z9999 No2</Part>
</Count>
</GetCountsResult>
</GetCountersResult>";XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlCounters); var countNodes = xmlDoc.GetElementsByTagName("Count"); //foreach throws, the element list has changed, error for (int i=0; i < countNodes.Count; i++) { xmlDoc.DocumentElement.ReplaceChild(countNodes\[i\], countNodes\[i\].ParentNode); } string fileName = @"F:\\ModifiedXml.xml"; xmlDoc.Save(fileName);
}
//The contents of file
// <GetCountersResult>
// <Count>
// <ID>FP108MR</ID>
// <Value>0</Value>
// <Part>Z9999</Part>
// </Count>
// <Count>
// <ID>FP108MR No2</ID>
// <Value>0 No2</Value>
// <Part>Z9999 No2</Part>
// </Count>
// </GetCountersResult>