English 中文(简体)
• 如何为清单执行规定XmlArrayItem的签字名称?
原标题:How do I Set XmlArrayItem Element name for a List<Custom> implementation?

我想建立一个习俗XML结构如下:

<Hotels>
    <Hotel />
</Hotels>

一. 创立了<代码>的实施。 名单仅能做到这一点。 我的法典如下:

[XmlRootAttribute(ElementName="Hotels")]
public class HotelList: List<HotelBasic>

由于名单所持有的类别没有名称Hotel,但HotelBasic本人xml

<Hotels>
   <HotelBasic />
</Hotels>

我如何在无需执行<条码>、可操作<>条码>或<条码>的情况下确定这一点?

最佳回答

假设你正在使用<代码>XmlSerializer,如果你想要这样做的话,那么你可以使用<代码>。 XmlTypeAttribute:

[XmlType(TypeName = "Hotel")]
public class HotelBasic
{
    public string Name { get; set; }
}

您的<代码>使用时 HotelList category it will be seriesized as:

<Hotels xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Hotel>
    <Name>One</Name>
  </Hotel>
  <Hotel>
    <Name>Two</Name>
  </Hotel>
</Hotels>
问题回答
[XmlArray("Hotels")]
[XmlArrayItem(typeof(Hotel), ElementName="Hotel")]
public HotelList[] Messages { get; set; }

生产:

<Hotels>
    <Hotel />
    <Hotel />
</Hotels>

[XmlRoot("Hotels")]
public class HotelList : IXmlSerializable
{
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        this.Hotels = XDocument.Load(reader)
                               .Select(h => new Hotel { Name = (string)h.Attribute("name") }
                               .ToList();
    }

    public void WriteXml(XmlWriter writer)
    {
        throw new NotSupportedException();
    }
}

我认为,马德0显示了你在这里最简单的选择,但只是为了完整,......我个人不建议“将名单作为根本目标”,因为各种原因(包括:我看到这些属性至少在平台上工作——可能已经是自由党或自由党可以记住)。 相反,我总是建议采用一种习俗:

[XmlRoot("Hotels")]
public class HotelResult // or something similar
{
    [XmlElement("Hotel")]
    public List<HotelBasic> Hotels { get { return hotel; } }

    private readonly List<HotelBasic> hotels = new List<HotelBasic>();
}

这将具有相同的xml结构,并具有更大的灵活性(青年可以增加其他特性/要素),并且将 t带(<条形码>;T>输入你的类型模型(优于继承)。





相关问题
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. ...

热门标签