write this xml in c#
What's the best way to produce this xml programmatically and persist to file? The data source is just going to be a csv file (you may suggest the csv file be formed another way if it makes programming the xml easier (flexible in this area)):
business name, address line
Comapny Name 1, 123 Main St.
Company Name 2, 1 Elm St.
Company Name 2, 2 Eml St.
<?xml version="1.0"?>
<ArrayOfBusiness xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Business>
<Name>Company Name 1</Name>
<AddressList>
<Address>
<AddressLine>123 Mai开发者_StackOverflow社区n St.</AddressLine>
</Address>
</AddressList>
</Business>
<Business>
<Name>Company Name 2</Name>
<AddressList>
<Address>
<AddressLine>1 Elm St.</AddressLine>
</Address>
<Address>
<AddressLine>2 Elm St.</AddressLine>
</Address>
</AddressList>
</Business>
</ArrayOfBusiness>
string path = @"C:\Path\To\Output.xml";
List<Business> list = // set data
using (var streamWriter = new StreamWriter(new FileStream(path, FileMode.Write)))
{
using (var xmlWriter writer = new XmlTextWriter(streamWriter))
{
var serialiser = new XmlSerializer(typeof(List<Business>));
serialiser.Serialize(xmlWriter, list);
}
}
I understand now that you are simply trying to convert the CSV file to the (inferred) XML schema above.
To follow on from the reply from Jason, this should accomplish most of it for you and act as starter code:
internal string Convert()
{
string[] lines = {
"Company Name 1, 123 Main St.",
"Company Name 2, 1 Elm St.",
"Company Name 2, 2 Elm St"
};
//var lines = File.ReadLines(path);
var builder = new StringBuilder();
foreach (var line in lines)
{
var fields = line.Split(',');
var settings = new XmlWriterSettings {OmitXmlDeclaration = true};
using (var writer = XmlWriter.Create(builder, settings))
{
//writer.WriteStartDocument();
writer.WriteStartElement("ArrayofBusiness");
writer.WriteStartElement("Business");
writer.WriteElementString("Name", fields[0]);
writer.WriteStartElement("AddressList");
writer.WriteStartElement("Address");
writer.WriteElementString("AddressLine", fields[1]);
writer.WriteEndElement();//Address
writer.WriteEndElement();//AddressList
writer.WriteEndElement();//Business
writer.WriteEndElement();//ArrayOfBusiness
//writer.WriteEndDocument();
}
}
return builder.ToString();
}
I can understand the difficulty in answering this question. There are many ways to recreate the xml but each technique has a pro and con depending on the system
for example:
- you could iterate the list and use the c# xml commands
- you could add xml attributes to your classes and properties and push them through a serialiser
- you could even spit the xml out directly from sql
On top of that we have to assume that the elements shown are all the elements available as you have not provided a schema.
Based on your comment, it looks like your question is actually about how to read, say, a CSV file and obtain a collection of Business
objects based on the file.
I'll assume a Business
class defined as follows:
class Business {
public string CompanyName { get; set; }
public string AddressLine { get; set; }
public Business(string companyName, string addressLine) {
this.CompanyName = companyName;
this.AddressLine = addressLine;
}
}
Then:
var businesses = new List<Business>();
var lines = File.ReadLines(path);
foreach(var line in lines) {
string[] fields = line.Split(',');
string companyName = fields[0];
string addressLine = fields[1];
Business business = new Business(companyName, addressLine);
businesses.Add(business);
}
Note that I'm assuming that no field contains an embedded comma.
精彩评论