Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

XmlDocument XmlNode.AppendChild(XmlNode) leaves out attributes

$
0
0

I am writing a C# Forms application that will edit an xml config file. I have a function that loads the current values into a DataGridView Control. They can then edit the current values or add more entries. I am having issue with adding more entries. I am using and XmlDocument object, making a call to CreateElement, giving the elements some attributes, then calling AppendChild on the parent I would like to add the child element to.

private void SaveMics_Click(object sender, EventArgs e) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("MicData.xml"); XmlNodeList mics = xDoc.GetElementsByTagName("mic"); int micRow = 0; XmlNode microphones = xDoc.SelectSingleNode("/root/microphones");

bool updating = true; foreach (DataGridViewRow row in dataGridView1.Rows) { /** Section Updates the entries already contained in the XMLDocument **/ if (micRow < mics.Count && updating) { mics[micRow].Attributes["number"].Value = row.Cells["micID"].Value.ToString(); mics[micRow].Attributes["cam1"].Value = row.Cells["cam1"].Value.ToString(); mics[micRow].Attributes["cam2"].Value = row.Cells["cam2"].Value.ToString(); mics[micRow].Attributes["cam1Pre"].Value = row.Cells["cam1Pre"].Value.ToString(); mics[micRow].Attributes["cam2Pre"].Value = row.Cells["cam2Pre"].Value.ToString(); micRow++; } /************************************************************* Here is where I add new Entries ************************************************************/ /** Section adds new entries to the XmlDocument **/ else { updating = false;//Set Flag because mics.count will increment if (!row.IsNewRow) { // Create new element XmlElement temp = xDoc.CreateElement("mic"); //Create new attributes XmlAttribute num = xDoc.CreateAttribute("number"); num.Value = row.Cells["micID"].Value.ToString(); XmlAttribute c1 = xDoc.CreateAttribute("cam1"); c1.Value = row.Cells["cam1"].Value.ToString(); XmlAttribute c2 = xDoc.CreateAttribute("cam2"); c2.Value = row.Cells["cam2"].Value.ToString(); XmlAttribute c1P = xDoc.CreateAttribute("cam1Pre"); c1P.Value = row.Cells["cam1Pre"].Value.ToString(); XmlAttribute c2P = xDoc.CreateAttribute("cam2Pre"); c2P.Value = row.Cells["cam2Pre"].Value.ToString(); //Append Attributes to the element temp.SetAttributeNode(num); temp.SetAttributeNode(c1); temp.SetAttributeNode(c2); temp.SetAttributeNode(c1P); temp.SetAttributeNode(c2P); /******** Not Properly Adding Element? Element is added without Attributes ***********/ microphones.AppendChild(temp); } } } /** If rows were removed inside dataGridView1, remove from XML **/ if (micRow < mics.Count) { while (micRow < mics.Count) { mics[micRow].RemoveAll(); micRow++; } } xDoc.Save("MicData.xml"); }

<?xml version="1.0" encoding="utf-8" ?><root><!--  List of Microphones --><microphones><mic number="1" cam1="3" cam2="4" cam1Pre="1" cam2Pre="1"/><mic number="2" cam1="3" cam2="4" cam1Pre="2" cam2Pre="2"/><mic number="3" cam1="3" cam2="4" cam1Pre="3" cam2Pre="3"/><mic number="4" cam1="3" cam2="4" cam1Pre="4" cam2Pre="4"/><mic number="5" cam1="1" cam2="2" cam1Pre="1" cam2Pre="1"/><mic number="6" cam1="1" cam2="2" cam1Pre="2" cam2Pre="2"/><mic number="7" cam1="1" cam2="2" cam1Pre="3" cam2Pre="3"/><mic number="8" cam1="1" cam2="2" cam1Pre="4" cam2Pre="4"/></microphones></root>

After Trying to add an element:

<?xml version="1.0" encoding="utf-8" ?><root><!--  List of Microphones --><microphones><mic number="1" cam1="3" cam2="4" cam1Pre="1" cam2Pre="1"/><mic number="2" cam1="3" cam2="4" cam1Pre="2" cam2Pre="2"/><mic number="3" cam1="3" cam2="4" cam1Pre="3" cam2Pre="3"/><mic number="4" cam1="3" cam2="4" cam1Pre="4" cam2Pre="4"/><mic number="5" cam1="1" cam2="2" cam1Pre="1" cam2Pre="1"/><mic number="6" cam1="1" cam2="2" cam1Pre="2" cam2Pre="2"/><mic number="7" cam1="1" cam2="2" cam1Pre="3" cam2Pre="3"/><mic number="8" cam1="1" cam2="2" cam1Pre="4" cam2Pre="4"/><mic /></microphones></root>





Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>