使用属性C#

时间:2016-07-10 20:36:15

标签: c# xml deserialization

我正在使用带有C#的NPR XML API。我有一个故事XML对象,有三个link个节点,唯一的区别是type属性。

<story id="485432424">
  <link type="html">http://www.npr.org/2016/07/10/485432424/with-administrative-corruption-in-afghanistan-u-s-troops-presence-won-t-make-any?ft=nprml&amp;f=1149</link>
  <link type="api">http://api.npr.org/query?id=485432424&amp;apiKey=MDIxNjY4ODAwMDE0NTAxMjAwODQ4ZTA1Nw000</link>
  <link type="short">http://n.pr/29EFodu</link>

我知道我可以将这些链接反序列化为数组,并调用类似story.Links[0]的内容来获取html链接版本,但这看起来很糟糕。最好在我的Story对象中直接使用HTML,API和Short属性,因此我可以像这样访问它:story.Link.HTMLstory.Link.API

有没有办法通过Microsoft XML库实现这一目标?不幸的是,我没有找到任何可以做这样的装饰工作。

2 个答案:

答案 0 :(得分:1)

这是一种古老的学校方式,但我会做类似下面的事情,只是创建我自己的班级结构。

class Program
{
    static void Main(string[] args)
    {
        string sXml = @"<story id=""485432424"">
                <link type=""html"">http://www.npr.org/2016/07/10/485432424/with-administrative-corruption-in-afghanistan-u-s-troops-presence-won-t-make-any?ft=nprml&amp;f=1149</link>
                <link type=""api"">http://api.npr.org/query?id=485432424&amp;apiKey=MDIxNjY4ODAwMDE0NTAxMjAwODQ4ZTA1Nw000</link>
                <link type=""short"">http://n.pr/29EFodu</link>
            </story>";

        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.LoadXml(sXml);

        Story story = new Story(doc.DocumentElement);

        Console.Write(story.ToString());
        Console.ReadLine();
    }
}

class Story
{
    public Link Link { get; set; }
    public Story(System.Xml.XmlNode nStory)
    {
        this.Link = new Link();
        foreach (System.Xml.XmlNode nLink in nStory.ChildNodes)
        {
            if (nLink.NodeType == System.Xml.XmlNodeType.Element)
            {
                this.Link.AddLink(nLink);
            }
        }
    }
    public override string ToString()
    {
        return new StringBuilder().Append(
            "Html: ").Append(this.Link.Html).Append(Environment.NewLine).Append(
            "Api: ").Append(this.Link.Api).Append(Environment.NewLine).Append(
            "Short: ").Append(this.Link.Short).Append(Environment.NewLine).ToString();
    }
}
class Link
{
    public string Html { get; set; }
    public string Api { get; set; }
    public string Short { get; set; }

    public Link()
    {
    }
    public void AddLink(System.Xml.XmlNode nLink)
    {
        switch (nLink.Attributes["type"].Value)
        {
            case "html":
                Html = nLink.InnerText;
                break;
            case "api":
                Api = nLink.InnerText;
                break;
            case "short":
                Short = nLink.InnerText;
                break;
        }
    }
}

答案 1 :(得分:1)

您可以使用XDocument

void Main()
{
    var xml = @"<story id=""485432424"">
      <link type=""html"">http://www.npr.org/2016/07/10/485432424/with-administrative-corruption-in-afghanistan-u-s-troops-presence-won-t-make-any?ft=nprml&amp;f=1149</link>
      <link type=""api"">http://api.npr.org/query?id=485432424&amp;apiKey=MDIxNjY4ODAwMDE0NTAxMjAwODQ4ZTA1Nw000</link>
      <link type=""short"">http://n.pr/29EFodu</link>
      </story>";

    using (var reader = new StringReader(xml))
    {
        var stories = new List<Story>();
        var xDoc = XDocument.Load(reader);
        foreach (var storyElement in xDoc.Elements("story"))
        {
            var story = new Story();
            foreach (var linkElement in storyElement.Elements("link"))
            {
                var value = linkElement.Attribute("type").Value;
                if (value == "html")
                    story.Html = linkElement.Value;
                else if (value == "api")
                    story.Api = linkElement.Value;
                else if (value == "short")
                    story.Short = linkElement.Value;
            }
            stories.Add(story);
        }

        // process stories;
    }
}

public class Story
{
    public string Html { get; set; }
    public string Api { get; set; }
    public string Short { get; set;}
}