1

I am trying to generate an .xml file using C#. I am using a class generated from an XSD for this purpose.

To be precise I am doing some experiment with Nunit results.xsd.

I need to generate some elements multiple times. I have below code at end of each function

 var serializer = new XmlSerializer(typeof(resultType));
  using (var stream = new StreamWriter(@"C:\test.xml",true))
serializer.Serialize(stream, data);

When I execute this multiple times, I am seeing the xml tag with XML version and encoding getting appended every time.

How can I put them as a individual functions so that I can call the objects multiple times for creating multiple XML elements of same type and in the end save the XML with data? I need the generated file to have structure similar to Nunit's results XML file.

3
  • 1
    I'm not sure I understand the question. Commented Sep 16, 2015 at 7:37
  • Also not sure I understand the question. A valid XML file must have a single root element, so you could save your results as a List<resultType>, if that's what you're looking to do. Commented Sep 16, 2015 at 7:44
  • @dbc xml has a valid root element. But the element underneath it can be of 1 or more occurences. I need a way to have as many elements as program need and then save the xml. This is what i meant. :) Commented Sep 16, 2015 at 8:34

1 Answer 1

1

Edit; We use a "proxy" class to change the outcome without altering the existing schemes.

using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Tool.Cons
{
class Result
{
    [XmlElement("test-suite")]
    public testsuiteType[] testsuite { get; set; }

    /// <remarks/>
    [XmlAttribute()]
    public string name { get; set; }

    /// <remarks/>
    [XmlAttribute()]
    public decimal total { get; set; }

    /// <remarks/>
    [XmlAttribute()]
    public decimal failures { get; set; }

    /// <remarks/>
    [XmlAttribute("not-run")]
    public decimal notrun { get; set; }

    /// <remarks/>
    [XmlAttribute()]
    public string date { get; set; }

    /// <remarks/>
    [XmlAttribute()]
    public string time { get; set; }
}

class Program
{
    static void AppendTestSuite(XDocument xdocument, testsuiteType suite)
    {
        var ser = new XmlSerializer(typeof(testsuiteType));
        using (var writer = xdocument.Root.CreateWriter())
        {
            // We need this for some reason...
            writer.WriteWhitespace("");
            ser.Serialize(writer, suite);
        }
    }

    static void Main(string[] args)
    {
        var res = GetTestResult();
        var xdoc = new XDocument();
        // Create the root element.
        using (var writer = xdoc.CreateWriter())           
            ser.Serialize(writer, res);

        // For testing purposes:
        AppendTestSuite(xdoc, res.testsuite);
        var _out = xdoc.ToString();
    }
}
}

You could run xsd.exe (In the Microsoft SDK folder) using the switch /c <path to xsd> to generate a strongly-typed class for you. It's perfect in quite some cases. However!! The structure that is created isn't your daily class building experience. The class is quite oddly build, so please pay attention when using it.

Sign up to request clarification or add additional context in comments.

12 Comments

Hi, Sorry if the question was confusing. I ran xsd.exe and generated a class file. Now using that class i want to populate data in xml using c#.
Use XmlSerializer for that. When initialising the XmlSerializer use "typeof(YourXsdClass)" as its parameter and call Serialize the instance. Or deserialise respectively.
Yes i tried using XmlSerializer. But xml version = 1.0 encoding =utf-8 " is getting encoded for each insertion .We are supposed to instantiate root element class right.There are many classes including partial classes in Nunit xsd.
Correct me if I'm wrong, you're running multiple tests and want to output all of those tests into one output xml file?
Yes exactly. Multiple tests will be run and each tests will be having multiple steps.
|

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.