简体   繁体   English

从远程XML文件检索数据并将其添加到datagrid

[英]Retrieving Data from a remote XML File and add to datagrid

I have an XML file that I have no write access to it .. I retrieve it online. 我有一个XML文件,对此我没有写权限。.我在线检索它。

I have the code below, which works fine, but I can't move forward .. 我有下面的代码,可以正常工作,但是我无法前进..

using System.Data;
using System.Xml.Linq;


try
{
    XDocument XMLFile = XDocument.Load(@"http://Domain/path/to/file.xml");
    MessageBox.Show("Remote File Loaded Successfully ...");
    var items = XMLFile.Descendants("item");
    int i = 0;
    foreach (var item in items)
    {
        i++;
        //var title = item.Descendants("title");
        //MessageBox.Show(title.ToString());
    }
    MessageBox.Show("Items Found: " + i);
}
catch(exception ex)
{
    MessageBox.Show("Error: " + ex.Message.ToString());
    MessageBox.Show("Error: " + ex.InnerException.ToString());
}

The problem I'm facing is in the foreach loop. 我面临的问题是在foreach循环中。 Every root element item has some child elements. 每个根元素项都有一些子元素。 I can't figure out how to retrieve those elements!! 我不知道如何检索那些元素!

Also, I have a grid view, I want to add the result to it, how can I achieve that?? 另外,我有一个网格视图,我想将结果添加到其中,如何实现?

Thank. 谢谢。

EDIT 编辑

XML Sample: XML示例:

<item>
    <title>Title</title>
    <link>http://domain/link</link>
    <description>Some Text</description>
    <pubDate>Wed, 05 Dec 2012 01:29:37 -0500</pubDate>
    <guid isPermaLink="false">Domain_text_INTEGER</guid>
    <category domain="http://domain/link">A</category>
    <category domain="http://domain/link">B</category>
    <category domain="http://domain/link">C</category>
    <category domain="http://domain/link">D</category>
    <category domain="http://domain/link">E</category>
    <coop:keyword>A</coop:keyword>
    <coop:keyword>B</coop:keyword>
    <coop:keyword>C</coop:keyword>
    <coop:keyword>D</coop:keyword>
    <coop:keyword>E</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>Text</coop:keyword>
    <coop:keyword>text integer</coop:keyword>
    <coop:keyword>Text</coop:keyword>
</item>

What I need from it: 我需要的是:

  • Title 标题
  • Link 链接
  • Description 描述
  • PubDate 发布日期
  • Guid (INTEGER) <= I can get the integer from the string OR text integer Guid(INTEGER)<=我可以从字符串或文本整数中获取整数

Thanks. 谢谢。

So on your XMLfile XDocument you've got the descendants. 因此,在XMLfile XDocument上,您有了后代。

Also create a collection 同时创建一个集合

List<someobject> list = new List<someobject>();

So in your loop 所以在你的循环中

foreach (var item in items)
{
    var someObject = new SomeObject()
    {
        Title = item.Element("title").Value
    }
}

Then bind your collection to the Grid 然后将您的集合绑定到网格

This MSDN topic could help you. 这个MSDN主题可以为您提供帮助。 On MSDN they are posted Walk-through of your problem. 他们在MSDN上发布了您的问题的演练 Both links suggest to use dataset for this: 两个链接都建议为此使用数据集:

DataSet dsAuthors = new DataSet("authors");
string filePath = "Complete path where you saved the XML file";
dsAuthors.ReadXml(filePath);
dataGrid1.DataSource = dsAuthors;
dataGrid1.DataMember = "authors";
dataGrid1.CaptionText = dataGrid1.DataMember;

I just copy/paste an example. 我只是复制/粘贴一个示例。 You need to modify it for your purposes. 您需要针对您的目的对其进行修改。

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

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