Write this xml in C #

What is the best way to create this xml programmatically and save the file? The data source will simply be a CSV file (you can assume that the csv file will be generated in another way if it simplifies xml programming (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 Main 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> 
+3
source share
4 answers

Now I understand that you are just trying to convert a CSV file to the (supposed) XML schema above.

To follow further from the answer from Jason, this should do most of this for you and act as a start 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();
    }
0
source
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);
  }
}
+4
source

. xml, pro con

:

  • # xml
  • xml
  • xml sql

, , - , .

0
source

Based on your comment, it seems your question is how to read, say, a CSV file and get a collection of Businessfile-based objects .

I am assuming a class Businessdefined 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);
}

Please note that I assume that no field contains an embedded comma.

0
source

Source: https://habr.com/ru/post/1776024/


All Articles