I'm trying to re-create an xml document. I'm reading an existing xml document and then trying to re-create it by using C#.
So far I was able to read and create shell of new doc.
I am looping through each child and adding it to the product node.
my xml file (source):
<?xml version="1.0" encoding="utf-8"?>
<DroneList>
<product>
<manufacturer>DJI</manufacturer>
<modelName>Phantom 3</modelName>
<estimatedPrice>699</estimatedPrice>
<flightTime>17 to 20</flightTime>
<modelSize>Medium</modelSize>
</product>
<product>
<manufacturer>DJI</manufacturer>
<modelName>Inspire 1</modelName>
<estimatedPrice>2899</estimatedPrice>
<flightTime>18 to 20</flightTime>
<modelSize>Large</modelSize>
</product>
</DroneList>
Note: There are multiple products under the root with unique item.
My goal with the C# program is to be able to:
- read the existing products from list
- add them to the new xml document
- add new items to the new xml document
- save xml document as a new xml file
my C# code is below:
private void btnChange_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILE_PATH); // load xml data
XmlDocument newDoc = new XmlDocument(); // create a new xmldocument object to hold new xml data
txtDisplay.Text += "****Reading Drone List**** \r\n";
// loaded existing xml data
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList children = root.ChildNodes;
txtDisplay.Text += "****Showing Existing Data**** \r\n children: " + children.Count + "\r\n";
for (int i = 0; i < children.Count; i++)
{
XmlElement product = newDoc.CreateElement("product");
product.SetAttribute("productId", i.ToString());
foreach (XmlNode child in children)
{
XmlElement manufacturer = newDoc.CreateElement("manufacturer");
XmlElement modelName = newDoc.CreateElement("modelName");
XmlElement estimatedPrice = newDoc.CreateElement("estimatedPrice");
XmlElement flightTime = newDoc.CreateElement("flightTime");
XmlElement modelSize = newDoc.CreateElement("modelSize");
manufacturer.InnerText = child["manufacturer"].InnerText;
modelName.InnerText = child["modelName"].InnerText;
estimatedPrice.InnerText = child["estimatedPrice"].InnerText;
flightTime.InnerText = child["flightTime"].InnerText;
modelSize.InnerText = child["modelSize"].InnerText;
product.AppendChild(manufacturer);
product.AppendChild(modelName);
product.AppendChild(estimatedPrice);
product.AppendChild(flightTime);
product.AppendChild(modelSize);
}
}
txtDisplay.Text += "****Showing new doc so far**** \r\n";
txtDisplay.Text += newDoc.InnerXml + "\r\n"; // display xml in the textbox
//txtDisplay.Text += newRoot.InnerText + "\r\n";
txtDisplay.Text += "****Adding New Data**** \r\n";
for (int i = 5; i < 5; i++)
{
//
}
XmlElement newRoot = newDoc.CreateElement("DroneList"); // the root element is <roster>
newDoc.AppendChild(newRoot);
XmlDeclaration xmlDec = newDoc.CreateXmlDeclaration("1.0", null, null);
newDoc.InsertBefore(xmlDec, newRoot); // insert before the root element
newDoc.Save("..\\..\\..\\new_drones.xml");
}
Desired XML outcome:
new xml file (target)
<?xml version="1.0" encoding="utf-8"?>
<DroneList>
<!-- existing products from source file -->
<product>
<manufacturer>DJI</manufacturer>
<modelName>Phantom 3</modelName>
<estimatedPrice>699</estimatedPrice>
<flightTime>17 to 20</flightTime>
<modelSize>Medium</modelSize>
</product>
<product>
<manufacturer>DJI</manufacturer>
<modelName>Inspire 1</modelName>
<estimatedPrice>2899</estimatedPrice>
<flightTime>18 to 20</flightTime>
<modelSize>Large</modelSize>
</product>
<!-- new products added through C# program (Hard coded) -->
<product>
<manufacturer>DJI</manufacturer>
<modelName>Inspire 1</modelName>
<estimatedPrice>2899</estimatedPrice>
<flightTime>18 to 20</flightTime>
<modelSize>Large</modelSize>
</product>
<product>
<manufacturer>DJI</manufacturer>
<modelName>Inspire 1</modelName>
<estimatedPrice>2899</estimatedPrice>
<flightTime>18 to 20</flightTime>
<modelSize>Large</modelSize>
</product>
<product>
<manufacturer>DJI</manufacturer>
<modelName>Inspire 1</modelName>
<estimatedPrice>2899</estimatedPrice>
<flightTime>18 to 20</flightTime>
<modelSize>Large</modelSize>
</product>
</DroneList>
Overall, XSLT is made by design to transform input XML into output XML, or other textual formats.
Please try the following XSLT based solution.
The XSLT takes the last product element and multiplies it 4 times. The rest of the input XML stays the same.
The c# code below is a generic boilerplate code for XSLT transformations.
Input XML
XSLT 1.0
Output XML
c#