如何更改XML根名称以XML序列化?

编程入门 行业动态 更新时间:2024-10-28 06:35:55
本文介绍了如何更改XML根名称以XML序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用C#做XML序列化时更改根名称。

I am trying to change the root name when doing XML serialization with C#.

它总是类名,而不是我想设置它的名字。

It always takes the class name and not the name I am trying to set it with.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyTest test = new MyTest(); test.Test = "gog"; List<MyTest> testList = new List<MyTest>() { test }; SerializeToXML(testList); } static public void SerializeToXML(List<MyTest> list) { XmlSerializer serializer = new XmlSerializer(typeof(List<MyTest>)); TextWriter textWriter = new StreamWriter(@"C:\New folder\test.xml"); serializer.Serialize(textWriter, list); textWriter.Close(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace ConsoleApplication1 { [XmlRootAttribute(ElementName = "WildAnimal", IsNullable = false)] public class MyTest { [XmlElement("Test")] public string Test { get; set; } } }

结果

Result

<?xml version="1.0" encoding="utf-8"?> <ArrayOfMyTest xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:xsd="www.w3/2001/XMLSchema"> <MyTest> <Test>gog</Test> </MyTest> </ArrayOfMyTest>

它不会将其更改为WildAnimal。我不知道为什么。我从一个教程得到这个。

It does not change it to WildAnimal. I am not sure why. I got this from a tutorial.

修改 @马克

感谢。我现在看到你做什么似乎只是这样奇怪的是,你必须让周围的包装。我有一个问题,如果我想使这种格式会发生什么

Thanks. I now see what your doing just seems so odd that you have to make a wrapper around it. I have one more question what happens if I wanted to make this format

<root> <element> <name></name> </element> <anotherElement> <product></product> </anotherElement> </root>

所以像一个嵌套的元素。我将不得不作出一个新的类为第二部分,坚持在包装类呢?

so like a nested elements. Would I have to make a new class for the second part and stick that in the wrapper class too?

推荐答案

在你〔实施例, MyTest的是不会根;你想在阵列重新命名?我会写一个包装:

In your exmaple, MyTest is not the root; are you trying to rename the array? I would write a wrapper:

[XmlRoot("NameOfRootElement")] public class MyWrapper { private List<MyTest> items = new List<MyTest>(); [XmlElement("NameOfChildElement")] public List<MyTest> Items { get { return items; } } } static void Main() { MyTest test = new MyTest(); test.Test = "gog"; MyWrapper wrapper = new MyWrapper { Items = { test } }; SerializeToXML(wrapper); } static public void SerializeToXML(MyWrapper list) { XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper)); using (TextWriter textWriter = new StreamWriter(@"test.xml")) { serializer.Serialize(textWriter, list); textWriter.Close(); } }

更多推荐

如何更改XML根名称以XML序列化?

本文发布于:2023-11-09 16:14:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   名称   序列化   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!