English 中文(简体)
对列表使用XmlSerializer时,我可以省略中间级别吗?
原标题:can i omit the intervening level when using XmlSerializer for a list?

我的问题最好用一个简单的例子来描述。考虑两个类,如下所示:

class Order {
  [XmlAttribute] int orderId;
  [XmlAttribute] int customerId;
  List<OrderItem> items;
}

class OrderItem {
  [XmlAttribute] int partCode;
  [XmlAttribute] int quantity;
}

使用XmlSerializer,这将序列化为如下内容:

<order orderId="...", customerId="..." >
  <Items>
    <orderItem partCode="..." quantity="..." />
  </Items>
</order>

我想做的是删除<;项目>;电平,使得<;订单项目>;元素在对应的<;订单>;

有办法做到这一点吗?

最佳回答

使用<code>XmlElement</code>属性:

class Order {
  [XmlAttribute] int orderId;
  [XmlAttribute] int customerId;
  [XmlElement]
  List<OrderItem> items;
}

使用此属性,您还可以为OrderItem对象指定自定义元素名称,甚至为OrderItem的每个子类型指定不同的元素名称

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签