简体   繁体   中英

How to change element name in C# xml serialize?

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 ".

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

you can check this msdn page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM