简体   繁体   English

xml与元素和属性一起C#

[英]xml with element and attribute together c#

How can I create a class in c# to map the following xml: 如何在c#中创建一个类以映射以下xml:

<Items>
<Item Name="Title">Title goes here</Item>
<Item Name="Description">Description goes here</Item>
<Item Name="Gender">Gender goes here</Item>
<Items>

I have tried many different things, I know how to set the element name and attribute name separately but I need them together as per the example. 我尝试了许多不同的方法,我知道如何分别设置元素名称和属性名称,但是根据示例,我需要它们一起使用。 Please note that this should not be an array. 请注意,这不应是数组。

I should be able to navigate through like: Items.Title = "title" 我应该能够像这样导航:Items.Title =“ title”

There is something important that needs to be added: the attributes Title, Description, Gender (plus about 10 attributes) are values that I am not passing. 有一些重要的事情需要添加:属性Title,Description,Gender(加上大约10个属性)是我没有传递的值。 This should "belong" to the model (class). 这应该“属于”模型(类)。 Hence why my example of accessing the object via Items.Title = "title" where Title is the property of the object that needs to receive the value. 因此,为什么我的示例通过Items.Title =“ title”访问对象,其中Title是需要接收值的对象的属性。

There is a utility that comes with Visual Studio called XSD.exe. Visual Studio附带有一个实用程序,称为XSD.exe。

You access it via the Visual Studio Developer command prompt. 您可以通过Visual Studio Developer命令提示符访问它。

Use this utility to generate your class(es) from the XML. 使用此实用程序从XML生成您的类。 Issuing the following command will give you all the options: 发出以下命令将为您提供所有选择:

XSD /?

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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