Hello guys,
here is what I did so far. I prefer the way you create in the first condition. however, i do not know how to append new elements in the similar way.
is there a way to reduce amount of code in 2nd condition?
Desired output:
Code:
here is what I did so far. I prefer the way you create in the first condition. however, i do not know how to append new elements in the similar way.
is there a way to reduce amount of code in 2nd condition?
Desired output:
Code:
<?xml version="1.0" encoding="utf-8" ?>
- <Definitions>
- <Definition>
<Acronym>a1</Acronym>
<Name>n1</Name>
- <Value>
<A>1</A>
<B>2</B>
<Alpha>3</Alpha>
<Beta>4</Beta>
</Value>
</Definition>
- <Definition>
<Acronym>test 2</Acronym>
<Name>test22</Name>
- <Value>
<A>551</A>
<B>23</B>
<Alpha>0.12</Alpha>
<Beta>-95.21</Beta>
</Value>
</Definition>
</Definitions>
- <Definitions>
- <Definition>
<Acronym>a1</Acronym>
<Name>n1</Name>
- <Value>
<A>1</A>
<B>2</B>
<Alpha>3</Alpha>
<Beta>4</Beta>
</Value>
</Definition>
- <Definition>
<Acronym>test 2</Acronym>
<Name>test22</Name>
- <Value>
<A>551</A>
<B>23</B>
<Alpha>0.12</Alpha>
<Beta>-95.21</Beta>
</Value>
</Definition>
</Definitions>
Code:
public void CreateXML_Definition(Definition definition)
{
string filename;
Stream xmlFile;
XmlDocument xmlDoc;
filename = "definitions.xml";
if (!File.Exists(filename))
{
xmlFile = new FileStream(filename, FileMode.Create);
using (XmlTextWriter xmlWriter = new XmlTextWriter(xmlFile, Encoding.UTF8))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Definitions");
xmlWriter.WriteStartElement("Definition");
xmlWriter.WriteElementString("Acronym", definition.DefinitionAcronym);
xmlWriter.WriteElementString("Name", definition.DefinitionName);
xmlWriter.WriteStartElement("Value");
xmlWriter.WriteElementString("A", definition.DefinitionValue.A.ToString());
xmlWriter.WriteElementString("B", definition.DefinitionValue.B.ToString());
xmlWriter.WriteElementString("Alpha", definition.DefinitionValue.Alpha.ToString());
xmlWriter.WriteElementString("Beta", definition.DefinitionValue.Beta.ToString());
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
else
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filename);
XmlElement subRoot = xmlDoc.CreateElement("Definition");
XmlElement acronym = xmlDoc.CreateElement("Acronym");
XmlText xmlAcronym = xmlDoc.CreateTextNode(definition.DefinitionAcronym);
acronym.AppendChild(xmlAcronym);
subRoot.AppendChild(acronym);
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement name = xmlDoc.CreateElement("Name");
XmlText xmlName = xmlDoc.CreateTextNode(definition.DefinitionName);
name.AppendChild(xmlName);
subRoot.AppendChild(name);
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement value = xmlDoc.CreateElement("Value");
subRoot.AppendChild(value);
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement xmlValue_A = xmlDoc.CreateElement("A");
XmlText val_A = xmlDoc.CreateTextNode(definition.DefinitionValue.A.ToString());
xmlValue_A.AppendChild(val_A);
value.AppendChild(xmlValue_A);
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement xmlValue_B = xmlDoc.CreateElement("B");
XmlText val_B = xmlDoc.CreateTextNode(definition.DefinitionValue.B.ToString());
xmlValue_B.AppendChild(val_B);
value.AppendChild(xmlValue_B);
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement xmlValue_C = xmlDoc.CreateElement("Alpha");
XmlText val_C = xmlDoc.CreateTextNode(definition.DefinitionValue.Alpha.ToString());
xmlValue_C.AppendChild(val_C);
value.AppendChild(xmlValue_C);
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement xmlValue_D = xmlDoc.CreateElement("Beta");
XmlText val_D = xmlDoc.CreateTextNode(definition.DefinitionValue.Beta.ToString());
xmlValue_D.AppendChild(val_D);
value.AppendChild(xmlValue_D);
xmlDoc.DocumentElement.AppendChild(subRoot);
xmlDoc.Save(filename);
}
}
Comment