• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Creating a XML file with arrays in C#

I have a question.

say I have 2 arrays.

arrayNames
arrayContent

so arrayNames has ProjectName, ProjectID, ProjectDate
and arrayContent has Server Implementation, 123, 05/30/07

I want to loop through and create a XML file. The other thing is, there are more then one project so I need to continue adding to the file once I start.

so the end product is

<Projects>
<Project>
<ProjectName>Server Implementation</ProjectName>
<ProjectID>123</ProjectID>
<ProjectDate>05/30/07</ProjectDate>
</Projects>
</Project>

Thanks
 
This is what I have so far, it's general

//***

using System;

using System.Xml;
using System.Collections;
namespace ReadXml1
{

class Class1
{


static void Main(string[] args)
{

//pick whatever filename with .xml extension
string filename = "XML"+DateTime.Now.Day + ".xml";
Console.WriteLine(DateTime.Now.Ticks);

XmlDocument xmlDoc = new XmlDocument();

try
{
xmlDoc.Load(filename);
}
catch(System.IO.FileNotFoundException)
{
//if file is not found, create a new xml file
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("Projects");
XmlElement childNode = xmlDoc.CreateElement("Project");
xmlWriter.Close();
xmlDoc.Load(filename);
}
string[] arrayNames = new string[]{"Server Impl", "123", "05/25/2007" };
string[] arrayContent = new string[] { "Project_Name", "Project_Number", "Project_Date" };
XmlNode root = xmlDoc.DocumentElement;

XmlElement childNode2 = xmlDoc.CreateElement("Project_Name");


root.AppendChild(childNode2);


xmlDoc.Save(filename);
}

}

}




 
Back
Top