如何将 XML 映射到 C# 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I map XML to C# objects
提问by Ragoler
I have an XML that I want to load to objects, manipulate those objects (set values, read values) and then save those XMLs back. It is important for me to have the XML in the structure (xsd) that I created.
我有一个 XML,我想将它加载到对象中,操作这些对象(设置值、读取值),然后将这些 XML 保存回来。在我创建的结构 (xsd) 中包含 XML 对我来说很重要。
One way to do that is to write my own serializer, but is there a built in support for it or open source in C# that I can use?
一种方法是编写我自己的序列化程序,但是我可以使用它的内置支持或 C# 中的开源吗?
采纳答案by ckarras
You can generate serializable C# classes from a schema (xsd) using xsd.exe:
您可以使用 xsd.exe 从架构 (xsd) 生成可序列化的 C# 类:
xsd.exe dependency1.xsd dependency2.xsd schema.xsd /out:outputDir
If the schema has dependencies (included/imported schemas), they must all be included on the same command line.
如果架构具有依赖项(包含/导入的架构),则它们必须全部包含在同一命令行中。
回答by dcstraw
I'll bet NetDataContractSerializer can do what you want.
我敢打赌 NetDataContractSerializer 可以做你想做的事。
回答by ljs
LINQ to XMLis very powerful if you're using .net 3.5, LINQ to XSDmay be useful to you too!
如果您使用 .net 3.5,LINQ to XML非常强大,LINQ to XSD也可能对您有用!
回答by Joel Coehoorn
Use xsd.exe command line program that comes with visual studio to create class files that you can use in your project/solution, and the System.Xml.Serialization namespace (specifically, the XmlSerializer class) to serialize/deserialze those classes to and from disk.
使用 Visual Studio 随附的 xsd.exe 命令行程序创建可在项目/解决方案中使用的类文件,以及 System.Xml.Serialization 命名空间(特别是 XmlSerializer 类)以将这些类序列化/反序列化盘。
回答by ckarras
using System.Xml.Serialization; this namespace has all the attributes you'll need if you want to map your xml to any random object. Alternatively you can use the xsd.exe tool
使用 System.Xml.Serialization;如果要将 xml 映射到任何随机对象,则此命名空间具有您需要的所有属性。或者,您可以使用 xsd.exe 工具
xsd file.xsd {/classes | /dataset} [/element:element] [/language:language] [/namespace:namespace] [/outputdir:directory] [URI:uri] which will take your xsd files and create c# or vb.net classes out of them.
xsd 文件.xsd {/classes | /dataset} [/element:element] [/language:language] [/namespace:namespace] [/outputdir:directory] [URI:uri] 它将获取您的 xsd 文件并从中创建 c# 或 vb.net 类。
http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx
http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx
回答by ckarras
This code (C# DotNet 1.0 onwards) works quite well to serialize most objects to XML. (and back) It does not work for objects containing ArrayLists, and if possible stick to using only Arrays
这段代码(从 C# DotNet 1.0 开始)可以很好地将大多数对象序列化为 XML。(并返回)它不适用于包含 ArrayLists 的对象,如果可能,坚持只使用 Arrays
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public static string Serialize(object objectToSerialize)
{
MemoryStream mem = new MemoryStream();
XmlSerializer ser = new XmlSerializer(objectToSerialize.GetType());
ser.Serialize(mem, objectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(mem.ToArray());
}
public static object Deserialize(Type typeToDeserialize, string xmlString)
{
byte[] bytes = Encoding.UTF8.GetBytes(xmlString);
MemoryStream mem = new MemoryStream(bytes);
XmlSerializer ser = new XmlSerializer(typeToDeserialize);
return ser.Deserialize(mem);
}
回答by binball
xsd.exe from Microsoft has a lot of bugs :| Try this open source pearl http://xsd2code.codeplex.com/
Microsoft 的 xsd.exe 有很多错误:| 试试这个开源珍珠http://xsd2code.codeplex.com/
回答by Savaratkar
We have created a framework which can auto-generate C# classes out of your XML. Its a visual item template to which you pass your XML and the classes are generated automatically in your project. Using these classes you can create/read/write your XML.
我们创建了一个框架,可以从您的 XML 中自动生成 C# 类。它是一个可视项模板,您可以将 XML 传递给它,并且在您的项目中会自动生成类。使用这些类,您可以创建/读取/写入您的 XML。
Check this link for the framework and Visual C# item template: click here
检查此框架和 Visual C# 项模板的链接:单击此处
回答by Steve Coleman
I agree xsd is really crap... But they made another version that hardly anyone knows about. Its called xsd object generator. Its the next version and has way more options. It generates files from XSD and works fantastic. If you have a schema generator like XML spy; create an xsd from your xml and use this tool. I have created very very complex classes using this tool. Then create partial classes for extra properties\methods etc, then when you update your schema you just regen your classes and any edits persist in your partial classes.
我同意 xsd 真的很垃圾......但他们制作了另一个几乎没人知道的版本。它称为 xsd 对象生成器。它的下一个版本,有更多的选择。它从 XSD 生成文件并且效果很好。如果你有一个像 XML spy 这样的模式生成器;从您的 xml 创建一个 xsd 并使用此工具。我使用这个工具创建了非常非常复杂的类。然后为额外的属性\方法等创建部分类,然后当您更新架构时,您只需重新生成您的类,并且任何编辑都会保留在您的部分类中。
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7075
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7075