I am trying to create XML file and write some data to it.
But I have a problem. I do not know how to write a value in this file. (I think writing a string
must be OK)
Here is my code
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
XmlElement element1 = doc.CreateElement(string.Empty, "Data", string.Empty);
doc.AppendChild(element1);
for (int i = 0; i < Program.Number; ++i)
{
XmlElement element2 = doc.CreateElement(string.Empty, "Block", string.Empty);
doc.AppendChild(element2);
XmlElement element3 = doc.CreateElement(string.Empty, "ID", string.Empty);
element3.AppendChild(i); //Compilator gives error there
element2.AppendChild(element3);
XmlElement element4 = doc.CreateElement(string.Empty, "Name", string.Empty);
XmlText text2 = doc.CreateTextNode(Colegue<string>.Name[i]);
element4.AppendChild(text2);
element2.AppendChild(element4)
}
doc.Save(Program.FileName);
I must get something like this after work of my program:
<Data>
<Block>
<ID>0</ID>
<Name>Rob</Name>
</Block>
<Block>
<ID>1</ID>
<Name>Peter</Name>
</Block>
</Data>
Can you help me? Thanks.
I would suggest you class serialization.
Make classes that reflect your data structure and serialize it. For example, classes can be like this:
[Serializable]
[XmlRoot("Data")]
public class Data
{
[XmlArray("Blocks")]
[XmlArrayItem("Block")]
public List<Block> Blocks { get; set; }
}
[Serializable]
public class Block
{
public string Name { get; set; }
public int ID { get; set; }
}
Attribute XmlRoot
tells serializer how to name root element while XmlArray
and XmlArrayItem
control how will list of Blocks and Block element will be serialized. When you fill your classes, just serialize them to xml.
Data data = new Data();
data.Blocks = new List<Block>();
data.Blocks.Add(new Block() { ID = 0, Name = "Rob" });
data.Blocks.Add(new Block() { ID = 1, Name = "Peter" });
data.Blocks.Add(new Block() { ID = 2, Name = "Mary" });
XmlSerializer xs = new XmlSerializer(typeof(Data));
using (FileStream stream = new FileStream("c:\\test.xml", FileMode.Create))
{
xs.Serialize(stream, data);
}
resulting XML
<?xml version="1.0"?>
<Data xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Blocks>
<Block>
<Name>Rob</Name>
<ID>0</ID>
</Block>
<Block>
<Name>Peter</Name>
<ID>1</ID>
</Block>
<Block>
<Name>Mary</Name>
<ID>2</ID>
</Block>
</Blocks>
</Data>
Use XmlDocument
this way:
class Program
{
static List<string> Colleagues = new List<string>() { "Rob", "Peter"};
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
XmlNode rootNode = doc.CreateElement("Data");
doc.AppendChild(rootNode);
for (int i = 0; i < 2; i++)
{
XmlNode blockNode = doc.CreateElement("Block");
XmlNode idNode = doc.CreateElement("ID");
idNode.InnerText = i.ToString();
blockNode.AppendChild(idNode);
XmlNode nameNode = doc.CreateElement("Name");
nameNode.InnerText = Colleagues[i];
blockNode.AppendChild(nameNode);
rootNode.AppendChild(blockNode);
}
doc.Save("XmlDocument.xml");
}
}
Output:
<Data>
<Block>
<ID>0</ID>
<Name>Rob</Name>
</Block>
<Block>
<ID>1</ID>
<Name>Peter</Name>
</Block>
</Data>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.