3

How would I go about doing this:

for( var i = 0; i < emp; i++ )
{
    Console.WriteLine("Name: ");
    var name = Console.ReadLine();

    Console.WriteLine("Nationality:");
    var country = Console.ReadLine();

    employeeList.Add( new Employee(){
                        Name = name,
                        Nationality = country
                     } );
}

I want a test run of, for example:

Imran Khan
Pakistani

to generate an XML File:

<employee>
   <name> Imran Khan </name>
   <nationality> Pakistani </nationality>
</employee>

Any suggestions?

2
  • possible duplicate of What is the best way to build XML in C# code? Commented Apr 2, 2012 at 10:59
  • Many ways to do this - serialization or direct output using custom classes or LINQ to XML (for example). Commented Apr 2, 2012 at 11:01

4 Answers 4

5

My suggestion is to use xml serialization:

[XmlRoot("employee")]
public class Employee {
    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("nationality")]
    public string Nationality { get; set; }
}

void Main() {
    // ...
    var serializer = new XmlSerializer(typeof(Employee));
    var emp = new Employee { /* properties... */ };
    using (var output = /* open a Stream or a StringWriter for output */) {
        serializer.Serialize(output, emp);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1, thinking of the third-party libraries for xml I used in C++, C# xml serialization looks irresistable.
3

There are several ways, but the one I like is using the class XDocument.

Here's a nice example on how to do it. How can I build XML in C#?

If you have any questions, just ask.

Comments

2

To give you an idea of how XDocument works based on your loop, you would do this:

XDocument xdoc = new XDocument();
xdoc.Add(new XElement("employees"));
for (var i = 0; i < 3; i++)
{
     Console.WriteLine("Name: ");
     var name = Console.ReadLine();

      Console.WriteLine("Nationality:");
      var country = Console.ReadLine();

      XElement el = new XElement("employee");
      el.Add(new XElement("name", name), new XElement("country", country));
      xdoc.Element("employees").Add(el);
}

After running, xdoc would be something like:

<employees>
  <employee>
    <name>bob</name>
    <country>us</country>
  </employee>
  <employee>
    <name>jess</name>
    <country>us</country>
  </employee>
</employees>

Comments

0
<employee>
   <name> Imran Khan </name>
   <nationality> Pakistani </nationality>
</employee>

XElement x = new  XElement ("employee",new XElement("name",e.name),new XElement("nationality",e.nationality) );

1 Comment

Code only answers are rarely helpful. Please elaborate on your code and explain why and how this solves the question.

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.