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

2
  • 3
    I would hope the file also has a root element - what you've shown isn't a valid XML document. Commented Oct 19, 2011 at 9:17
  • Also see this question stackoverflow.com/questions/2948255/… Commented Oct 19, 2011 at 9:30

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your quick response. I would need an output more like this: <Account value="test"> <Partner value="2144"> <City value="Munsbach"></City> <City value="Schuttrange"></City> </Partner> </Account> An idea? Best, Jeppen
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"?
The example I provided here is what I would like to have as an output:<Account value="test"> <Partner value="2144"> <City value="Munsbach"></City> <City value="Schuttrange"></City> </Partner> </Account> It is the result I'm getting from SQL. Can't do much about it.
@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.