zonkerman
asked on
How Do I Delete An XML Node And Its Children With C# Without Leaving An Empty Element Behind?
Hello.
I am trying to delete an XML node and all its children without leaving behind an empy element. Below is the xml before the delete:
<?xml version="1.0" encoding="utf-8"?>
<WorkTeams>
<WorkTeam Name="BlueTeam">
<HomeBase>Los Angeles</HomeBase>
<Vehicles>35</Vehicles>
<VehicleMfgs>
<VehicleMfg>Toyota</Vehicl eMfg>
<VehicleMfg>Nissan</Vehicl eMfg>
<VehicleMfg>Volvo</Vehicle Mfg>
</VehicleMfgs>
</WorkTeam>
<WorkTeam Name="RedTeam">
<HomeBase>San Diego</HomeBase>
<Vehicles>20</Vehicles>
<VehicleMfgs>
<VehicleMfg>Ford</VehicleM fg>
<VehicleMfg>Dodge</Vehicle Mfg>
<VehicleMfg>Suzuki</Vehicl eMfg>
</VehicleMfgs>
</WorkTeam>
</WorkTeams>
I am trying to delete the node for <WorkTeam Name="RedTeam"> and all its children. I have written the following C# code to do this but the problem with it is that it leaves behind an empy node:
XmlDocument anXmlDoc = new XmlDocument();
const string XmlFilePath = "c:\\workteams.xml";
const string XmlFileModifiedPath = "c:\\workteams2.xml";
anXmlDoc.Load(XmlFilePath) ;
XmlNodeList anXmlNodeListOfWorkTeam = anXmlDoc.GetElementsByTagN ame("WorkT eam");
XmlNode anXMLNodeForWorkTeam = anXmlNodeListOfWorkTeam[0] .SelectSin gleNode("/ WorkTeams/ WorkTeam[@ Name='RedT eam']");
if (anXMLNodeForWorkTeam != null)
{
anXMLNodeForWorkTeam.Remov eAll();
anXmlDoc.Save(XmlFileModif iedPath);
}
After running the above C# program the XML ends up looking as follows:
<?xml version="1.0" encoding="utf-8"?>
<WorkTeams>
<WorkTeam Name="BlueTeam">
<HomeBase>Los Angeles</HomeBase>
<Vehicles>35</Vehicles>
<VehicleMfgs>
<VehicleMfg>Toyota</Vehicl eMfg>
<VehicleMfg>Nissan</Vehicl eMfg>
<VehicleMfg>Volvo</Vehicle Mfg>
</VehicleMfgs>
</WorkTeam>
<WorkTeam>
</WorkTeam>
</WorkTeams>
At the bottom I am left with an empty node showing as
<WorkTeam>
</WorkTeam>
How can I perform the delete without ending up with this empty node? A code example solving the above example is what I am looking for. Thanks in advance
I am trying to delete an XML node and all its children without leaving behind an empy element. Below is the xml before the delete:
<?xml version="1.0" encoding="utf-8"?>
<WorkTeams>
<WorkTeam Name="BlueTeam">
<HomeBase>Los Angeles</HomeBase>
<Vehicles>35</Vehicles>
<VehicleMfgs>
<VehicleMfg>Toyota</Vehicl
<VehicleMfg>Nissan</Vehicl
<VehicleMfg>Volvo</Vehicle
</VehicleMfgs>
</WorkTeam>
<WorkTeam Name="RedTeam">
<HomeBase>San Diego</HomeBase>
<Vehicles>20</Vehicles>
<VehicleMfgs>
<VehicleMfg>Ford</VehicleM
<VehicleMfg>Dodge</Vehicle
<VehicleMfg>Suzuki</Vehicl
</VehicleMfgs>
</WorkTeam>
</WorkTeams>
I am trying to delete the node for <WorkTeam Name="RedTeam"> and all its children. I have written the following C# code to do this but the problem with it is that it leaves behind an empy node:
XmlDocument anXmlDoc = new XmlDocument();
const string XmlFilePath = "c:\\workteams.xml";
const string XmlFileModifiedPath = "c:\\workteams2.xml";
anXmlDoc.Load(XmlFilePath)
XmlNodeList anXmlNodeListOfWorkTeam = anXmlDoc.GetElementsByTagN
XmlNode anXMLNodeForWorkTeam = anXmlNodeListOfWorkTeam[0]
if (anXMLNodeForWorkTeam != null)
{
anXMLNodeForWorkTeam.Remov
anXmlDoc.Save(XmlFileModif
}
After running the above C# program the XML ends up looking as follows:
<?xml version="1.0" encoding="utf-8"?>
<WorkTeams>
<WorkTeam Name="BlueTeam">
<HomeBase>Los Angeles</HomeBase>
<Vehicles>35</Vehicles>
<VehicleMfgs>
<VehicleMfg>Toyota</Vehicl
<VehicleMfg>Nissan</Vehicl
<VehicleMfg>Volvo</Vehicle
</VehicleMfgs>
</WorkTeam>
<WorkTeam>
</WorkTeam>
</WorkTeams>
At the bottom I am left with an empty node showing as
<WorkTeam>
</WorkTeam>
How can I perform the delete without ending up with this empty node? A code example solving the above example is what I am looking for. Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello Jandromeda.
Your code does all nodes related to RedTeam properly. Thanks! Looks like you might have forgotten to add the save statement but you did plan to implement it because of the XMLFileModifiedPath variable so I simply added that one statement
anXmlDoc.Save(XmlFileModif iedPath);
to the end of your sample as follows:
private void button2_Click(object sender, EventArgs e)
{
XmlDocument anXmlDoc = new XmlDocument();
const string XmlFilePath = "c:\\workteams.xml";
const string XmlFileModifiedPath = "c:\\workteams2.xml";
anXmlDoc.Load(XmlFilePath) ;
XmlNode removeNode = anXmlDoc.SelectSingleNode( "//WorkTea m[@Name='R edTeam']") ;
anXmlDoc.DocumentElement.R emoveChild (removeNod e);
anXmlDoc.Save(XmlFileModif iedPath);
}
Your code does all nodes related to RedTeam properly. Thanks! Looks like you might have forgotten to add the save statement but you did plan to implement it because of the XMLFileModifiedPath variable so I simply added that one statement
anXmlDoc.Save(XmlFileModif
to the end of your sample as follows:
private void button2_Click(object sender, EventArgs e)
{
XmlDocument anXmlDoc = new XmlDocument();
const string XmlFilePath = "c:\\workteams.xml";
const string XmlFileModifiedPath = "c:\\workteams2.xml";
anXmlDoc.Load(XmlFilePath)
XmlNode removeNode = anXmlDoc.SelectSingleNode(
anXmlDoc.DocumentElement.R
anXmlDoc.Save(XmlFileModif
}
I intentionally did not included it as you already know how to save an Xml document. I'm really sorry if that cause you any inconveniences.
Happy coding! :)
Happy coding! :)
if (anXMLNodeForWorkTeam != null)
{
anXmlNodeListOfWorkTeam[0]
anXmlDoc.Save(XmlFileModif
}