I need to remove elements in some xml files. How do I correctly ensure that I transverse through all childnodes to remove all element with a matching name. I have code such as:
string strFilename = #"D:\sample.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFilename);
XmlNode parentNode;
parentNode = xmlDoc.DocumentElement;
foreach (XmlNode node1 in parentNode.ChildNodes)
{
foreach (XmlNode node2 in node1.ChildNodes)
{
if (node2.Name == "ElementNodeToDelete")
{
node2.ParentNode.RemoveChild(node2);
}
foreach (XmlNode node3 in node2.ChildNodes)
{
if (node3.Name == "ElementNodeToDelete")
{
node3.ParentNode.RemoveChild(node3);
}
foreach (XmlNode node4 in node3.ChildNodes)
{
if (node4.Name == "ElementNodeToDelete")
{
node4.ParentNode.RemoveChild(node4);
}
}
}
}
}
Is this a valid solution? How do I determine how many foreach loops to perform to ensure that all children are checked?
Thanks for your time in advance.
Recursion is your friend.
void DeleteNodes(XmlNode root, string deleteName)
{
foreach(XmlNode node in root.ChildNodes)
{
if (node.Name == deleteName)
{
root.RemoveChild(node);
}
else
{
DeleteNodes(node,deleteName);
}
}
}
Related
After loading elements and attributes of the below xml file in to a treeview, the nodes are edited and the treeview is saved back in to the same xml file. All elements and attributes need to be saved. However the attributes of only nested elements disappear during saving. After saving, all attribures of elements d & e are lost! This is because i am unable to retrieve the attribute values stored in to the tag property in addTreeNode function.(please see inline comments) Does anyone know of an easier or cleaner way to achieve this? Providing code snippets would be helpfull.
XML structure:
<?xml version="1.0" encoding="utf-8"?>
<root>
<a axa="1" axb="2" axc="3">content_of_tag _a</a>
<b bxa="10" bxb="20" bxc="30">content_of_tag_b</b>
<c cxa="11" cxb="21" cxc="31">
content_of_tag_c
<d dxa="101" dxb="201" dxc="301">
content_of_tag_d
<e exa="110" exb="210" exc="310">
content_of_tag_e
</e>
</d>
</c>
</root>
C# code:
private void Xml2TreeNode(XElement xNode, TreeNode treeNode)
{
if (xNode.HasElements) //if node has children
{
TreeNode tNode = null;
int i = 0;
foreach (XElement subNode in xNode.Elements())
{
if (subNode.Descendants().Count() > 0)
{
TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim());
tn.Nodes.Add(new TreeNode(subNode.FirstNode.ToString().Trim()));
treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //-------->this attribure values are NOT retrievable in saveNodes function!
tNode = tn; //add child nodes
}
else
{
TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim()); //show name of a leaf node element
tn.Nodes.Add(new TreeNode(subNode.Value.ToString().Trim())); //show value of above element as a child of its name
treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrivable in saveNodes function
tNode = treeNode.Nodes[i++]; //add sibling node
}
addTreeNode(subNode, tNode); //recursively add child nodes
}
}
}
private void TreeNode2Xml(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
if (node.Nodes.Count > 0)
{
xr.WriteStartElement(node.Text);
if (node.Tag != null) //attribures retrieved here
{
List<XAttribute> attributeList = node.Tag as List<XAttribute>;
foreach (XAttribute attribute in attributeList)
{
xr.WriteAttributeString(attribute.Name.ToString(), attribute.Value.ToString());
}
}
saveNodes(node.Nodes);
xr.WriteEndElement();
}
else //No child nodes, so we just write the text
{
xr.WriteString(node.Text);
}
}
}
xr = new XmlTextWriter(filename, System.Text.Encoding.UTF8); //System.Text.Encoding.UTF8
xr.Formatting = Formatting.Indented;
xr.WriteStartDocument();
//Write our root node
xr.WriteStartElement(treeView1.Nodes[0].Text);
foreach (TreeNode node in tv.Nodes)
{
**TreeNode2Xml(node.Nodes);**
}
//Close the root node
xr.WriteEndElement();
xr.Close();
xDoc = XDocument.Load(dlg.FileName);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xDoc.Document.Root.Name.ToString().Trim()));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)treeView1.Nodes[0];
**Xml2TreeNode(xDoc.Root, tNode);**
treeView1.ExpandAll();
Exelent post of load and saving of data of treeview
Example Load/Save XML-Treeview
Save Data Code:
//We use this in the export and the saveNode
//functions, though it's only instantiated once.
private StreamWriter sr;
public void exportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
//Write the header
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
//Write our root node
sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
foreach (TreeNode node in tv.Nodes)
{
saveNode(node.Nodes);
}
//Close the root node
sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
sr.Close();
}
private void saveNode(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
//If we have child nodes, we'll write
//a parent node, then iterrate through
//the children
if (node.Nodes.Count > 0)
{
sr.WriteLine("<" + node.Text + ">");
saveNode(node.Nodes);
sr.WriteLine("</" + node.Text + ">");
}
else //No child nodes, so we just write the text
sr.WriteLine(node.Text);
}
}
Load Data
//Open the XML file, and start to populate the treeview
private void populateTreeview()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open XML Document";
dlg.Filter = "XML Files (*.xml)|*.xml";
dlg.FileName = Application.StartupPath + "\\..\\..\\example.xml";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
//Just a good practice -- change the cursor to a
//wait cursor while the nodes populate
this.Cursor = Cursors.WaitCursor;
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(dlg.FileName);
//Now, clear out the treeview,
//and add the first (root) node
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new
TreeNode(xDoc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)treeView1.Nodes[0];
//We make a call to addTreeNode,
//where we'll add all of our nodes
addTreeNode(xDoc.DocumentElement, tNode);
//Expand the treeview to show all nodes
treeView1.ExpandAll();
}
catch(XmlException xExc)
//Exception is thrown is there is an error in the Xml
{
MessageBox.Show(xExc.Message);
}
catch(Exception ex) //General exception
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default; //Change the cursor back
}
}
}
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for(int x=0; x<=xNodeList.Count-1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
treeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNode.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Text = xmlNode.OuterXml.Trim();
}
Fixed the issue by making small changes in Xml2TreeNode() function
private void Xml2TreeNode(XElement xNode, TreeNode treeNode)
{
if (xNode.HasElements) //if node has children
{
TreeNode tNode = null;
int i = 0;
foreach (XElement subNode in xNode.Elements())
{
if (subNode.Descendants().Count() > 0)
{//handle non-leaf node
TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim());
tn.Nodes.Add(new TreeNode(subNode.FirstNode.ToString().Trim()));
tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in SaveNodes function
tNode = tn; //add child nodes
}
else
{//handle leaf node
TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim()); //show name of a leaf node element
tn.Nodes.Add(new TreeNode(subNode.Value.ToString().Trim())); //show value of above element as a child of its name
tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in SaveNodes function
tNode = treeNode.Nodes[i++]; //add sibling node
}
Xml2TreeNode(subNode, tNode); //recursively add child nodes
}
}
}
private void Xml2TreeNode(XElement xNode, TreeNode treeNode)
{
if (xNode.HasElements) //if node has children
{
TreeNode tNode = null;
int i = 0;
foreach (XElement subNode in xNode.Elements())
{
if (subNode.Descendants().Count() > 0)
{//handle non-leaf node
TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim());
////tn.Nodes.Add(new TreeNode(subNode.FirstNode.ToString().Trim())); //adds extra element-value to node
tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in TreeNode2Xml function
tNode = tn; //add child nodes
}
else
{//handle leaf node
TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim()); //show name of a leaf node element
tn.Nodes.Add(new TreeNode(subNode.Value.ToString().Trim())); //show value of above element as a child of its name
tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in TreeNode2Xml function
tNode = treeNode.Nodes[i++]; //add sibling node
}
Xml2TreeNode(subNode, tNode); //recursively add child nodes
}
}
}
I would like to distinguish these two cases - but HasChildNode returns in both cases "true" and the number of childNodes is 1 in both cases.
<eventid>45072</eventid>
<titles>
<title>kabel eins late news</title>
</titles>
Here ist my sample code and output.
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XmlNodeList xnList = doc.SelectNodes("/epg/programme");
foreach (XmlNode n in xnList) {
foreach (XmlNode n1 in n.ChildNodes) {
if (n1.HasChildNodes) {
AppendText($"n1 has {n1.ChildNodes.Count} ChildNodes");
foreach (XmlNode n2 in n1.ChildNodes) {
Append(41,n2.Name, n2.InnerText);
}
}
else {
// this will never called
Append(46,n1.Name, n1.InnerText);
}
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<epg Ver="1">
<programme>
<eventid>45072</eventid>
<titles>
<title>kabel eins late news</title>
</titles>
</programme>
</epg>
Output:
n1 has 1 ChildNodes
41:#text:45072
n1 has 1 ChildNodes
41:title:kabel eins late news
Thanks to #DavidBrowne-Microsoft I have changed my code to XDocument and a little bit Linq to XML
XDocument doc = XDocument.Parse(xmlString);
var programme = doc.Descendants("programme").Select(p => p);
foreach (XElement p in programme) {
foreach (var attr in p.Attributes()){
AppendText($"{attr.Name} {attr.Value}");
}
foreach (XElement ele in p.Elements()) {
if (ele.HasElements) {
foreach (XElement child in ele.Elements()) {
AppendText($"{child.Name} {child.Value}");
}
}
else {
AppendText($"{ele.Name} {ele.Value}");
}
}
}
I found only a few beginner examples so here is this snippet for the start.
I am doing a migration tool for sharepoint sites. I migrate sites using XML files. In my method for generating my XML file, however, I get an infinite loop and I can't figure out the cause. I get no errors what so ever but the XML file gets understandably large.
How I create the root node of my XML file:
private void createRootXmlNodeFromRootSite()
{
if (rootsite != null)
{
//Add root-site to TreeView
XmlElement rootnode = document.CreateElement(strRoot);
rootnode.SetAttribute(strTitle, rootsite.Title);
rootnode.SetAttribute(strName, rootsite.SiteAdministrators[0].Name);
rootnode.SetAttribute(strEmail, rootsite.SiteAdministrators[0].Email);
document.AppendChild(rootnode);
XmlElement nodelists = document.CreateElement(strLists);
if (rootsite.Lists.Count > 0)
{
rootnode.AppendChild(nodelists);
//Get all lists within root-site and put them under it's node.
foreach (SPList list in rootsite.Lists)
{
XmlElement nodelist = document.CreateElement(strList);
nodelist.SetAttribute(strTitle, list.Title);
if (!list.Hidden)
{
nodelists.AppendChild(nodelist);
XmlElement nodects = document.CreateElement(strContentTypes);
if (list.ContentTypes.Count > 0)
{
nodelist.AppendChild(nodects);
//Get all content types within lists of root site and put them under it's node.
foreach (SPContentType ct in list.ContentTypes)
{
XmlElement nodect = document.CreateElement(strContentType);
nodect.SetAttribute(strTitle, ct.Name);
if (!ct.Hidden)
{
nodects.AppendChild(nodect);
XmlElement nodefields = document.CreateElement(strFields);
if (ct.Fields.Count > 0)
{
nodect.AppendChild(nodefields);
foreach (SPField field in ct.Fields)
{
XmlElement nodefield = document.CreateElement(strField);
nodefield.SetAttribute(strTitle, field.Title);
if (!field.Hidden)
{
nodefields.AppendChild(nodefield);
}
}
}
}
}
}
}
//Call recursive methods to get child-site-elements.
convertSiteCollectionToXml(rootnode, rootsite);
}
}
}
}
This method then calls a recursive method which creates child nodes from the subsites:
private void convertSiteCollectionToXml(XmlNode nodeElement, SPWeb site)
{
XmlElement nodesites = document.CreateElement("Sites");
if (site.Webs.Count > 0)
{
nodeElement.AppendChild(nodesites);
foreach (SPWeb childsite in site.Webs)
{
XmlElement child = document.CreateElement("Site");
child.SetAttribute("Title", childsite.Title);
nodesites.AppendChild(child);
XmlElement nodelists = document.CreateElement("Lists");
if (childsite.Lists.Count > 0)
{
child.AppendChild(nodelists);
foreach (SPList list in childsite.Lists)
{
XmlElement nodelist = document.CreateElement("List");
nodelist.SetAttribute("Title", list.Title);
if (!list.Hidden)
{
nodelists.AppendChild(nodelist);
XmlElement nodects = document.CreateElement("ContentTypes");
if (list.ContentTypes.Count > 0)
{
nodelist.AppendChild(nodects);
foreach (SPContentType ct in list.ContentTypes)
{
XmlElement nodect = document.CreateElement("ContentType");
nodect.SetAttribute("Title", ct.Name);
if (!ct.Hidden)
{
nodects.AppendChild(nodect);
XmlElement nodefields = document.CreateElement("Fields");
if (ct.Fields.Count > 0)
{
nodect.AppendChild(nodefields);
foreach (SPField field in ct.Fields)
{
XmlElement nodefield = document.CreateElement("Field");
nodefield.SetAttribute("Title", field.Title);
if (!field.Hidden)
{
nodefields.AppendChild(nodefield);
}
}
}
}
}
}
}
convertSiteCollectionToXml(child, childsite);
}
}
}
}
}
The infinite loop happens in the second method. Anyone see the cause?
I have this XML
<?xml version="1.0" encoding="UTF-8"?>
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">-<NFe>-<infNFe versao="2.00" Id="NFe35130649196462000115550010000036141000025758">
<ide>
<natOp>DEVOL. ARMAZENAGEM</natOp>
</ide>
<total>
<ICMSTot>
<vNF>43778.00</vNF>
</ICMSTot>
</total>
I read with this C# code :
private void leerarquivoN(string caminx)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(caminx);
XmlNodeList ml = xmlDoc.GetElementsByTagName("*");
XmlNode primer = xmlDoc.DocumentElement;
tipo_arq = primer.Name;
if (xmlDoc.DocumentElement.Name == "nfeProc")
{ foreach (XmlNode xn in ml)
{
if (xn.Name == "infNFe")
{
chave_nota = xn.Attributes["Id"].Value;
versao = xn.Attributes["versao"].Value;
lblChaveNota.Text = chave_nota; lblversao.Text = versao;
}
}
XmlNodeList xnList = xmlDoc.GetElementsByTagName("ide");
foreach (XmlNode xn in xnList)
{
if (xn.Name == "ide")
{
if (xn["nNF"] != null)
{ nnota = (xn["nNF"]).InnerText; label8.Text = nnota; }
if (xn["dEmi"] != null)
{ ndata = (xn["dEmi"]).InnerText; lblData.Text = ndata;}
if (xn["natOP"] != null)
{ natop = (xn["natOP"]).InnerText; lblNAtop.Text = natop ; }
}
}
XmlNodeList xnList2 = xmlDoc.GetElementsByTagName("emit");
foreach (XmlNode xn in xnList2)
{
{ if (xn["CNPJ"] != null)
{ ncnpj = (xn["CNPJ"]).InnerText; lblCNPJ.Text = ncnpj; }
}
}
XmlNodeList xnList3 = xmlDoc.GetElementsByTagName("total");
foreach (XmlNode xn in xnList3)
{
{
if (xn["vNF"] != null)
{ ntotal = (xn["vNF"]).InnerText; lblvNF.Text = ntotal ; }
}
}
}
I can not read VNF and natOP , what i m doing wrong???
Thanks
The code you are using just gets the main nodes. To get the children associated with each node (natOp and vNF), you have to rely on a different code (MSDN reference). Sample for natOp (inside the xnList for loop):
if (xn.HasChildNodes)
{
for (int i=0; i<xn.ChildNodes.Count; i++)
{
//xn.ChildNodes[i].InnerText -> what you are after
}
}
I am trying to loop through an Xml file and display the value for account in a message.
XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var");
foreach (XmlNode no in nodeList)
{
XmlNode node = testDoc.SelectSingleNode("/details/row/var[#name='account']");
test.actual = node.Attributes["value"].Value;
MessageBox.Show(test.account);
}
The message box is currently displaying the first record repeatidly, how can I get to the next record?
Thanks for your input in advance.
You are repeatedly assigning node with the same element from testDoc. It is not clear what test.account is (perhaps a mistype for test.actual)?
no is the variable which will iterate the contents of nodeList - I imagine you intended to use that.
EDIT following edit of OP
Now you've shown us what nodeList is, I suspect you want to do something like this instead :
XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var[#name='account']");
foreach (XmlNode no in nodeList)
{
test.actual = no.Attributes["value"].Value;
...
XmlDocument doc = new XmlDocument();
doc.Load("d:\\test.xml");
XmlNodeList node = doc.GetElementsByTagName("w:r");
foreach (XmlNode xn in node)
{
try
{
if (xn["w:t"].InnerText != null)
{
if (xn["w:t"].InnerText == "#")
{
string placeHolder = xn["w:t"].InnerText;
foreach (XmlNode a in node)
{
if (a["w:t"].InnerText != "#")
{
string placeHolder1 = a["w:t"].InnerText;
}
}
}
}
}
catch (Exception e)
{
Console.Write(e);
}
}
Here is the sample for parent node value to get information of the child nodes.here i am using the ReportItems ParentNode and Print only image child nodes.
xmldoc.Load(rdlFile);
StringBuilder sb=new StringBuilder();
XmlNode node = xmldoc.GetElementsByTagName("ReportItems")[0];
XmlNodeList list = node.ChildNodes;
atributes=new string[node.ChildNodes.Count];
int l = 0;
for (int j = 0; j < node.ChildNodes.Count; j++)
{
if (list[j].Name == "Image")
{
XmlAttributeCollection att = list[j].Attributes;
atributes[l] = att[0].Value.ToUpper();
}
l++;
}
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (searchText.Text.ToUpper() == atributes[i])
{
XmlNodeList lastlist = node.ChildNodes;
XmlNodeList endlist = lastlist[i].ChildNodes;
for (int k = 0; k < endlist.Count; k++)
{
sb.Append(endlist[k].Name+" - "+ endlist[k].InnerText);
sb.Append("\n"+"\n");
}
}
}
let me know if you have doubt..
Try this,
XmlDocument xdoc = new XDocument();
xdoc.Load("*/File/*");
string xmlcontents = xdoc.InnerXml;
var xpath = "(/details/row/var[#name='account'])";
XmlNodeList lists = xdoc.DocumentElement.SelectNodes(xpath);
foreach (XmlNode _node in lists)
{
string _nodeValue = _node.InnerText;
MessageBox.Show(_nodeValue);
}
Try the following:
//Create an xml reader;
XmlDocument _xmlDocument = new XmlDocument();
_xmlDocument.Load(/*File Name here*/);
//Select the element with in the xml you wish to extract;
XmlNodeList _nodeList = _xmlDocument.SelectNodes("/details/row/var[#name='account']");
//Display the values in the node list to the screen;
foreach (XmlNode _node in _nodeList)
{
String _nodeValue = _node.InnerText.ToString();
MessageBox.Show(_nodeValue.ToString());
}
I'm not 100% sure, but you may need to use recursion. If not, it should just look like this:
XmlDocument doc = //etc..
foreach(XmlNode node in doc.ChildNodes)
{
if(node.Name == "account")
{
MessageBox.Show(node.Value);
}
}
You shouldn't spend time with reading the xml node by node. Try Deserialization: