设计强烈建议XML类型的对象 [英] Design Strongly typed object from XML
问题描述
我有几个,我需要使用的XML文件,我一直使用的XElement对象,并通过属性名称或的XElement的价值拉升的数据。
I have a couple of XML files that I need to work with, and I've always used the XElement objects and pulled the data via the attribute name or the XElement's value.
我知道必须有在C#中使用XML的更好的方法。什么是要么自动生成或手动生成XML文件中一个强类型对象的最佳方式是什么?
I know there has to be a better way of using XML in C#. What is the best way to either auto generate or manually generate a strong typed object from the XML file?
XML被
<Group id="####">
<title>Some Title</title>
<description>some description</description>
<Rule id="ID_##" severity="medium" weight="10.0">
<version>1.001</version>
<title>Another Title</title>
<description>Very long description</description>
<fixtext fixref="F-31r1_fix">Description of fix</fixtext>
<fix id="F-31r1_fix"/>
<check system="C-7883r4_chk">
<check-content-ref name="M" href="URI"/>
<check-content>Content</check-content>
</check>
</Rule>
</Group>
如果我能解析XML文件转换成列表<组>
这将是最好的。
If I could parse an XML file into a List<Group>
that would be the best.
我能想到这样做是手动创建组,规则和检查对象和手动分配的唯一方法数据。如果有一个更好,更自动化的方式来做到这一点,请让我知道!
The only way I can think to do it is manually create the Group, Rule, and Check objects and manually assign the data. If there is a better, more automated way to do this, please let me know!
推荐答案
您可以使用XSD.EXE(它可与Windows SDK安装,但在不同的位置取决于版本 - 矿井,目前在 C:\Program文件(x86)\Microsoft SDKs\Windows\v7.0A\Bin
)来生成和XSD从文件,或CS代码文件
You can use xsd.exe (which installs with the windows SDK, but in various locations depending on version - mine is currently in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
) to generate and xsd from the file, or a cs code file.
例如:
xsd.exe myFile.xml
将得到您在XML文件的位置myFile.xsd。
will get you myFile.xsd in the location of the xml file.
然后,
xsd.exe myFile.xsd /c
将让你一个与定义的类的.cs
文件。尝试 XSD.EXE /?
全选项(可以指定命名空间等为好)。
will get you a .cs
file with the classes defined. Try xsd.exe /?
for full options (you can specify namespaces, etc. as well).
这篇关于设计强烈建议XML类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!