1

I am defined a simple class and serialized it:

    public class Test
    {
        public string Name { set; get; }
    }

I am serized this simple object,the code like this:

                Test test = new Test();
                test.Name = "a";                    
                TextWriter writer = new StreamWriter(@"D:\a.xml");
                XmlSerializer s = new XmlSerializer(typeof(Test), "");
                s.Serialize(writer, test);
                writer.Close();

The a.xml result file like this:

<Test xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
  <Name>a</Name>
</Test>

That's no problem,but now i want my xml node content like this(change the default element name(like: Test) to user define name,whatever the name is(like: job-scheduling-data)):

<job-scheduling-data xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
  <Name>a</Name>
</job-scheduling-data>

What can i do to make it right? I don't want my class name like "job-scheduling-data".

2
  • You may want to use decorator attribute?[XmlElement(Name="somename")] Commented Jan 18, 2016 at 7:56
  • There's a whole section of MSDN devoted to XML Serialization, such as Controlling XML Serialization Using Attributes Commented Jan 18, 2016 at 7:59

1 Answer 1

2
[XmlRoot(ElementName = "job-scheduling-data")]
public class Test
{
    public string Name { set; get; }
}

you can check this msdn page.

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

2 Comments

Yes,it works.Thank you.I have tried this way for so much times,but my another class is very complex and it has no effect.
what do you mean by complex? If you mean inheritance, if you check the msdn page you will see the related configuration related to derived types.

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.