简体   繁体   English

如何使用属性正确地对XML节点建模?

[英]How to properly model an XML node with Attributes?

I'm trying to consume an XML feed and convert it into a model. 我正在尝试使用XML提要并将其转换为模型。

Some of the XML looks like this. 一些XML看起来像这样。

<guid isPermaLink="false">
    http://example.com/foo/bar
</guid>

In trying to model this, I'm using 在尝试对此建模时,我正在使用

[XmlElement("guid")]
public string Guid { get; set; }

[XmlElement("guid")]
public m_Guid Guid { get; set; }
public class m_Guid
{
    [XmlAttribute("isPermaLink")]
    public bool isPermaLink { get; set; }
}

but obviously VS is throwing an error 但是很明显VS抛出错误

This member is defined more than once. 多次定义了该成员。

I'm simply trying to figure out how to model this so that at the end of it all, I can use 我只是想弄清楚如何对此建模,以便最终可以使用

var theGuid = someItem.Guid;
var guidIsPermaLink = someItem.Guid.isPermaLink;

Just hoping someone can help point me in the right direction. 只是希望有人可以帮助我指出正确的方向。 I'm kind of new to modeling like this. 我是这种建模的新手。


Here's another example of confusing markup that needs to be modeled in a similar way. 这是另一个令人困惑的标记示例,需要以类似的方式进行建模。

<link>http://example.com/foo/bar/</link>
<atom:link rel="self" type="application/rss+xml" href="http://example.com/foo/bar/&format=rss"/>

needs to be modeled so that we can do this 需要建模,以便我们可以做到这一点

var link = someItem.Link;
var linkType = someItem.Link.type;
var linkHref = someItem.Link.href;

You could model it like this: 您可以像这样建模:

public class Guid 
{
    [XmlAttribute]
    public bool IsPermaLink { get; set; }

    // and the element value
    [XmlTextAttribute]
    public string Value;
}

public class Item
{
    [XmlElement]
    public Guid Guid { get; set; }
}
...
var theGuid = someItem.Guid.Value;
var guidIsPermaLink = someItem.Guid.IsPermaLink;

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

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