0

I'm trying to create a XML file using XDocument in C#.

The file has the following structure:

< acc Account="test" Partner="2144" CITY="Munsbach" />

< acc Account="test" Partner="2144" CITY="(Schuttrange" />

< acc Account="test" Partner="2145" CITY="Rumelange" />

< acc Account="test" Partner="2145" CITY="Belvaux" />

< acc Account="test" Partner="2145" CITY="Sassel" />

I added the linebreaks manually for better reading.

Could anyone help me please?

Thanks, Jeppen

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Jeppen
  • 414
  • 5
  • 12
  • 3
    I would hope the file also has a root element - what you've shown isn't a valid XML document. – Jon Skeet Oct 19 '11 at 09:17
  • Also see this question http://stackoverflow.com/questions/2948255/xml-file-creation-using-xdocument-in-c-sharp – stombeur Oct 19 '11 at 09:30

1 Answers1

1

I would prefer writing a data class like this:

    [Serializable]
    [XmlRoot("acc")]
    public class Account
    {
        [XmlElement("Account")]
        public string Account { get; set; }

        [XmlElement("Partner")]
        public int Partner { get; set; }

        [XmlElement("CITY")]
        public string City { get; set; }
    }

and serialize / deserialize it with XmlSerializer.

        List<AccountClass> accounts = new List<AccountClass> 
        { 
            new AccountClass { Account = "test", Partner = 2144, City = "Munsbach" }, 
            new AccountClass { Account = "test", Partner = 2144, City = "Schuttrange" } 
        };

        XmlSerializer ser = new XmlSerializer(typeof(List<AccountClass>));
        using (FileStream fileStream = new FileStream("File.xml", FileMode.OpenOrCreate))
        {
            ser.Serialize(fileStream, accounts);
        }

Your file will look like this:

<?xml version="1.0"?>
<ArrayOfAccountClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AccountClass>
    <Account>test</Account>
    <Partner>2144</Partner>
    <CITY>Munsbach</CITY>
  </AccountClass>
  <AccountClass>
    <Account>test</Account>
    <Partner>2144</Partner>
    <CITY>Schuttrange</CITY>
  </AccountClass>
</ArrayOfAccountClass>
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • Thanks for your quick response. I would need an output more like this: An idea? Best, Jeppen – Jeppen Oct 19 '11 at 10:25
  • That is totally different to the file you've asked for in your question! And it makes it much more difficult for you. That attribute "value" makes no really sense. Will you have more data to be serialized for a value like "City"? – Fischermaen Oct 19 '11 at 11:42
  • The example I provided here is what I would like to have as an output: It is the result I'm getting from SQL. Can't do much about it. – Jeppen Oct 19 '11 at 13:10
  • @Jeppen: What exactly are you going to do? Your question looked like "converting data into a xml file". Now you say, that you get an xml-file from SQL. Do you want to create data objects out of it? – Fischermaen Oct 19 '11 at 13:14