将嵌套 XML 绑定到 DataGridView
Bind nested XML to DataGridView
我通过 HttpWebRequest 获得 XML,然后搜索我需要的值,列出它们并在 DataGridView 上显示。
经过大量搜索后,我发现我的 XML 无法轻松绑定到 DataGridView,因为它是嵌套的,我需要先格式化此数据。
我有以下 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<feed xml:lang="de" xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:tel="http://tel.search.ch/api/spec/result/1.0/">
<id>https://tel.search.ch/api/MEINPRIVATERAPIKEY/68e7af8f8efa353de6d0b05f798598f4</id>
<title type="text">tel.search.ch API Search Results</title>
<generator version="1.0" uri="https://tel.search.ch">tel.search.ch</generator>
<updated>2007-03-22T03:00:00Z</updated>
<link href="https://tel.search.ch/result.html?name=john+meier&maxnum=2" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/api/?was=john+meier&maxnum=2&key=MEINPRIVATERAPIKEY" type="application/atom+xml" rel="self" />
<link href="https://tel.search.ch/api/?was=john+meier&maxnum=2&pos=3&key=MEINPRIVATERAPIKEY" rel="next" type="application/atom+xml" />
<openSearch:totalResults>14</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>2</openSearch:itemsPerPage>
<openSearch:Query role="request" searchTerms="john meier" startPage="1" />
<entry>
<id>urn:uuid:b4f420fda52419f2</id>
<updated>2007-03-22T03:00:00Z</updated>
<published>2007-03-22T03:00:00Z</published>
<title type="text">Meier, John</title>
<content type="text">Meier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30</content>
<autor>
<name>tel.search.ch</name>
</autor>
<link href="https://tel.search.ch/detail/b4f420fda52419f2" title="Details" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/vcard/Meier.vcf?key=b4f420fda52419f2" type="text/x-vcard" title="VCard Download" rel="alternate" />
<link href="https://tel.search.ch/edit/?id=b4f420fda52419f2" rel="edit" type="text/html" />
<tel:pos>1</tel:pos>
<tel:id>b4f420fda52419f2</tel:id>
<tel:type>Person</tel:type>
<tel:name>Meier</tel:name>
<tel:firstname>John</tel:firstname>
<tel:occupation></tel:occupation>
<tel:street>Marienfeldstrasse</tel:street>
<tel:streetno>92</tel:streetno>
<tel:zip>8252</tel:zip>
<tel:city>Schlatt</tel:city>
<tel:canton>TG</tel:canton>
<tel:phone>+41526544230</tel:phone>
</entry>
<entry>
<id>urn:uuid:c8c043412a3ce526</id>
<updated>2007-03-22T03:00:00Z</updated>
<published>2007-03-22T03:00:00Z</published>
<title type="text">John Meier IT-Consulting</title>
<content type="text">John Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92</content>
<autor>
<name>tel.search.ch</name>
</autor>
<link href="https://tel.search.ch/detail/c8c043412a3ce526" title="Details" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/vcard/Meier.vcf?key=c8c043412a3ce526" type="text/x-vcard" title="VCard Download" rel="alternate" />
<link href="https://tel.search.ch/edit/?id=c8c043412a3ce526" rel="edit" type="text/html" />
<tel:pos>2</tel:pos>
<tel:id>c8c043402a3ce526</tel:id>
<tel:type>Organisation</tel:type>
<tel:name>John Meier IT Consulting</tel:name>
<tel:occupation>Your Personal IT-Consultant</tel:occupation>
<tel:street>Unterdorfstrasse</tel:street>
<tel:streetno>22</tel:streetno>
<tel:zip>4143</tel:zip>
<tel:city>Dornach</tel:city>
<tel:canton>SO</tel:canton>
<tel:category>Software Grosshandel</tel:category>
<tel:category>Software & Consulting</tel:category>
<tel:phone>+41617236292</tel:phone>
<tel:extra type="fax">+41617236393</tel:extra>
<tel:extra type="mobile">+41763341010</tel:extra>
<tel:extra type="email">john.meier@mymail.com</tel:extra>
<tel:extra type="website">http://www.johnmeierconsult.com</tel:extra>
</entry>
</feed>
我的代码:
// Get XML from API
var url = "https://tel.search.ch/examples/api-response.xml";
var doc = new XDocument();
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
doc = XDocument.Load(resp.GetResponseStream());
}
// Format Data
var query = from el in doc.Descendants("entry")
select new
{
Position = el.Element("pos").Value,
Id = el.Element("id").Value,
Type = el.Element("type").Value,
Name = el.Element("name").Value,
Firstname = el.Element("firstname").Value,
Occupation = el.Element("occupation").Value,
Street = el.Element("street").Value,
StreetNo = el.Element("streetno").Value,
Zip = el.Element("zip").Value,
City = el.Element("city").Value,
Canton = el.Element("canton").Value,
Phone = el.Element("phone").Value
};
// Bind Query to DataGridView
dataGridView1.DataSource = query.ToList();
我尝试显示一行,其中包含 XML 中每个条目的所有可能值。
以这种方式调试时:
var result = from xml in doc.Descendants()
select xml;
foreach (var element in result)
{
Console.WriteLine("Element {0} = {1}",
element.Name, element.Value);
}
我可以看到以下数据:
Element {http://www.w3.org/2005/Atom}feed = https://tel.search.ch/api/MEINPRIVATERAPIKEY/68e7af8f8efa353de6d0b05f798598f4tel.search.ch API Search Resultstel.search.ch2007-03-22T03:00:00Z1412urn:uuid:b4f420fda52419f22007-03-22T03:00:00Z2007-03-22T03:00:00ZMeier, JohnMeier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30tel.search.ch1b4f420fda52419f2PersonMeierJohnMarienfeldstrasse928252SchlattTG+41526544230urn:uuid:c8c043412a3ce5262007-03-22T03:00:00Z2007-03-22T03:00:00ZJohn Meier IT-ConsultingJohn Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92tel.search.ch2c8c043402a3ce526OrganisationJohn Meier IT ConsultingYour Personal IT-ConsultantUnterdorfstrasse224143DornachSOSoftware GrosshandelSoftware & Consulting+41617236292+41617236393+41763341010john.meier@mymail.comhttp://www.johnmeierconsult.com
Element {http://www.w3.org/2005/Atom}id = https://tel.search.ch/api/MEINPRIVATERAPIKEY/68e7af8f8efa353de6d0b05f798598f4
Element {http://www.w3.org/2005/Atom}title = tel.search.ch API Search Results
Element {http://www.w3.org/2005/Atom}generator = tel.search.ch
Element {http://www.w3.org/2005/Atom}updated = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://a9.com/-/spec/opensearchrss/1.0/}totalResults = 14
Element {http://a9.com/-/spec/opensearchrss/1.0/}startIndex = 1
Element {http://a9.com/-/spec/opensearchrss/1.0/}itemsPerPage = 2
Element {http://a9.com/-/spec/opensearchrss/1.0/}Query =
Element {http://www.w3.org/2005/Atom}entry = urn:uuid:b4f420fda52419f22007-03-22T03:00:00Z2007-03-22T03:00:00ZMeier, JohnMeier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30tel.search.ch1b4f420fda52419f2PersonMeierJohnMarienfeldstrasse928252SchlattTG+41526544230
Element {http://www.w3.org/2005/Atom}id = urn:uuid:b4f420fda52419f2
Element {http://www.w3.org/2005/Atom}updated = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}published = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}title = Meier, John
Element {http://www.w3.org/2005/Atom}content = Meier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30
Element {http://www.w3.org/2005/Atom}autor = tel.search.ch
Element {http://www.w3.org/2005/Atom}name = tel.search.ch
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://tel.search.ch/api/spec/result/1.0/}pos = 1
Element {http://tel.search.ch/api/spec/result/1.0/}id = b4f420fda52419f2
Element {http://tel.search.ch/api/spec/result/1.0/}type = Person
Element {http://tel.search.ch/api/spec/result/1.0/}name = Meier
Element {http://tel.search.ch/api/spec/result/1.0/}firstname = John
Element {http://tel.search.ch/api/spec/result/1.0/}occupation =
Element {http://tel.search.ch/api/spec/result/1.0/}street = Marienfeldstrasse
Element {http://tel.search.ch/api/spec/result/1.0/}streetno = 92
Element {http://tel.search.ch/api/spec/result/1.0/}zip = 8252
Element {http://tel.search.ch/api/spec/result/1.0/}city = Schlatt
Element {http://tel.search.ch/api/spec/result/1.0/}canton = TG
Element {http://tel.search.ch/api/spec/result/1.0/}phone = +41526544230
Element {http://www.w3.org/2005/Atom}entry = urn:uuid:c8c043412a3ce5262007-03-22T03:00:00Z2007-03-22T03:00:00ZJohn Meier IT-ConsultingJohn Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92tel.search.ch2c8c043402a3ce526OrganisationJohn Meier IT ConsultingYour Personal IT-ConsultantUnterdorfstrasse224143DornachSOSoftware GrosshandelSoftware & Consulting+41617236292+41617236393+41763341010john.meier@mymail.comhttp://www.johnmeierconsult.com
Element {http://www.w3.org/2005/Atom}id = urn:uuid:c8c043412a3ce526
Element {http://www.w3.org/2005/Atom}updated = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}published = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}title = John Meier IT-Consulting
Element {http://www.w3.org/2005/Atom}content = John Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92
Element {http://www.w3.org/2005/Atom}autor = tel.search.ch
Element {http://www.w3.org/2005/Atom}name = tel.search.ch
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://tel.search.ch/api/spec/result/1.0/}pos = 2
Element {http://tel.search.ch/api/spec/result/1.0/}id = c8c043402a3ce526
Element {http://tel.search.ch/api/spec/result/1.0/}type = Organisation
Element {http://tel.search.ch/api/spec/result/1.0/}name = John Meier IT Consulting
Element {http://tel.search.ch/api/spec/result/1.0/}occupation = Your Personal IT-Consultant
Element {http://tel.search.ch/api/spec/result/1.0/}street = Unterdorfstrasse
Element {http://tel.search.ch/api/spec/result/1.0/}streetno = 22
Element {http://tel.search.ch/api/spec/result/1.0/}zip = 4143
Element {http://tel.search.ch/api/spec/result/1.0/}city = Dornach
Element {http://tel.search.ch/api/spec/result/1.0/}canton = SO
Element {http://tel.search.ch/api/spec/result/1.0/}category = Software Grosshandel
Element {http://tel.search.ch/api/spec/result/1.0/}category = Software & Consulting
Element {http://tel.search.ch/api/spec/result/1.0/}phone = +41617236292
Element {http://tel.search.ch/api/spec/result/1.0/}extra = +41617236393
Element {http://tel.search.ch/api/spec/result/1.0/}extra = +41763341010
Element {http://tel.search.ch/api/spec/result/1.0/}extra = john.meier@mymail.com
Element {http://tel.search.ch/api/spec/result/1.0/}extra = http://www.johnmeierconsult.com
我真的很迷茫,希望得到任何帮助。
您正在返回 null。我修改了代码来处理命名空间 a remove "Value" ,它在 item 为空时给出异常。而是转换为 string
sing System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication94
{
class Program
{
static void Main(string[] args)
{
var url = "https://tel.search.ch/examples/api-response.xml";
var doc = new XDocument();
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
doc = XDocument.Load(resp.GetResponseStream());
}
XNamespace ns = doc.Root.GetDefaultNamespace();
XNamespace telNs = doc.Root.GetNamespaceOfPrefix("tel");
// Format Data
var query = from el in doc.Descendants(ns + "entry")
select new
{
Position = (string)el.Element(telNs + "pos"),
Id = (string)el.Element(telNs + "id"),
Type = (string)el.Element(telNs + "type"),
Name = (string)el.Element(telNs + "name"),
Firstname = (string)el.Element(telNs + "firstname"),
Occupation = (string)el.Element(telNs + "occupation"),
Street = (string)el.Element(telNs + "street"),
StreetNo = (string)el.Element(telNs + "streetno"),
Zip = (string)el.Element(telNs + "zip"),
City = (string)el.Element(telNs + "city"),
Canton = (string)el.Element(telNs + "canton"),
Phone = (string)el.Element(telNs + "phone")
email = el.Elements(telNs + "extra").Where(x => (string)x.Attribute("type") == "email").Select(x => (string)x).ToList() //use FirstOrDefault() if you only want one.
};
var list = query.ToList();
}
}
}
我通过 HttpWebRequest 获得 XML,然后搜索我需要的值,列出它们并在 DataGridView 上显示。
经过大量搜索后,我发现我的 XML 无法轻松绑定到 DataGridView,因为它是嵌套的,我需要先格式化此数据。
我有以下 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<feed xml:lang="de" xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:tel="http://tel.search.ch/api/spec/result/1.0/">
<id>https://tel.search.ch/api/MEINPRIVATERAPIKEY/68e7af8f8efa353de6d0b05f798598f4</id>
<title type="text">tel.search.ch API Search Results</title>
<generator version="1.0" uri="https://tel.search.ch">tel.search.ch</generator>
<updated>2007-03-22T03:00:00Z</updated>
<link href="https://tel.search.ch/result.html?name=john+meier&maxnum=2" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/api/?was=john+meier&maxnum=2&key=MEINPRIVATERAPIKEY" type="application/atom+xml" rel="self" />
<link href="https://tel.search.ch/api/?was=john+meier&maxnum=2&pos=3&key=MEINPRIVATERAPIKEY" rel="next" type="application/atom+xml" />
<openSearch:totalResults>14</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>2</openSearch:itemsPerPage>
<openSearch:Query role="request" searchTerms="john meier" startPage="1" />
<entry>
<id>urn:uuid:b4f420fda52419f2</id>
<updated>2007-03-22T03:00:00Z</updated>
<published>2007-03-22T03:00:00Z</published>
<title type="text">Meier, John</title>
<content type="text">Meier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30</content>
<autor>
<name>tel.search.ch</name>
</autor>
<link href="https://tel.search.ch/detail/b4f420fda52419f2" title="Details" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/vcard/Meier.vcf?key=b4f420fda52419f2" type="text/x-vcard" title="VCard Download" rel="alternate" />
<link href="https://tel.search.ch/edit/?id=b4f420fda52419f2" rel="edit" type="text/html" />
<tel:pos>1</tel:pos>
<tel:id>b4f420fda52419f2</tel:id>
<tel:type>Person</tel:type>
<tel:name>Meier</tel:name>
<tel:firstname>John</tel:firstname>
<tel:occupation></tel:occupation>
<tel:street>Marienfeldstrasse</tel:street>
<tel:streetno>92</tel:streetno>
<tel:zip>8252</tel:zip>
<tel:city>Schlatt</tel:city>
<tel:canton>TG</tel:canton>
<tel:phone>+41526544230</tel:phone>
</entry>
<entry>
<id>urn:uuid:c8c043412a3ce526</id>
<updated>2007-03-22T03:00:00Z</updated>
<published>2007-03-22T03:00:00Z</published>
<title type="text">John Meier IT-Consulting</title>
<content type="text">John Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92</content>
<autor>
<name>tel.search.ch</name>
</autor>
<link href="https://tel.search.ch/detail/c8c043412a3ce526" title="Details" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/vcard/Meier.vcf?key=c8c043412a3ce526" type="text/x-vcard" title="VCard Download" rel="alternate" />
<link href="https://tel.search.ch/edit/?id=c8c043412a3ce526" rel="edit" type="text/html" />
<tel:pos>2</tel:pos>
<tel:id>c8c043402a3ce526</tel:id>
<tel:type>Organisation</tel:type>
<tel:name>John Meier IT Consulting</tel:name>
<tel:occupation>Your Personal IT-Consultant</tel:occupation>
<tel:street>Unterdorfstrasse</tel:street>
<tel:streetno>22</tel:streetno>
<tel:zip>4143</tel:zip>
<tel:city>Dornach</tel:city>
<tel:canton>SO</tel:canton>
<tel:category>Software Grosshandel</tel:category>
<tel:category>Software & Consulting</tel:category>
<tel:phone>+41617236292</tel:phone>
<tel:extra type="fax">+41617236393</tel:extra>
<tel:extra type="mobile">+41763341010</tel:extra>
<tel:extra type="email">john.meier@mymail.com</tel:extra>
<tel:extra type="website">http://www.johnmeierconsult.com</tel:extra>
</entry>
</feed>
我的代码:
// Get XML from API
var url = "https://tel.search.ch/examples/api-response.xml";
var doc = new XDocument();
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
doc = XDocument.Load(resp.GetResponseStream());
}
// Format Data
var query = from el in doc.Descendants("entry")
select new
{
Position = el.Element("pos").Value,
Id = el.Element("id").Value,
Type = el.Element("type").Value,
Name = el.Element("name").Value,
Firstname = el.Element("firstname").Value,
Occupation = el.Element("occupation").Value,
Street = el.Element("street").Value,
StreetNo = el.Element("streetno").Value,
Zip = el.Element("zip").Value,
City = el.Element("city").Value,
Canton = el.Element("canton").Value,
Phone = el.Element("phone").Value
};
// Bind Query to DataGridView
dataGridView1.DataSource = query.ToList();
我尝试显示一行,其中包含 XML 中每个条目的所有可能值。
以这种方式调试时:
var result = from xml in doc.Descendants()
select xml;
foreach (var element in result)
{
Console.WriteLine("Element {0} = {1}",
element.Name, element.Value);
}
我可以看到以下数据:
Element {http://www.w3.org/2005/Atom}feed = https://tel.search.ch/api/MEINPRIVATERAPIKEY/68e7af8f8efa353de6d0b05f798598f4tel.search.ch API Search Resultstel.search.ch2007-03-22T03:00:00Z1412urn:uuid:b4f420fda52419f22007-03-22T03:00:00Z2007-03-22T03:00:00ZMeier, JohnMeier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30tel.search.ch1b4f420fda52419f2PersonMeierJohnMarienfeldstrasse928252SchlattTG+41526544230urn:uuid:c8c043412a3ce5262007-03-22T03:00:00Z2007-03-22T03:00:00ZJohn Meier IT-ConsultingJohn Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92tel.search.ch2c8c043402a3ce526OrganisationJohn Meier IT ConsultingYour Personal IT-ConsultantUnterdorfstrasse224143DornachSOSoftware GrosshandelSoftware & Consulting+41617236292+41617236393+41763341010john.meier@mymail.comhttp://www.johnmeierconsult.com
Element {http://www.w3.org/2005/Atom}id = https://tel.search.ch/api/MEINPRIVATERAPIKEY/68e7af8f8efa353de6d0b05f798598f4
Element {http://www.w3.org/2005/Atom}title = tel.search.ch API Search Results
Element {http://www.w3.org/2005/Atom}generator = tel.search.ch
Element {http://www.w3.org/2005/Atom}updated = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://a9.com/-/spec/opensearchrss/1.0/}totalResults = 14
Element {http://a9.com/-/spec/opensearchrss/1.0/}startIndex = 1
Element {http://a9.com/-/spec/opensearchrss/1.0/}itemsPerPage = 2
Element {http://a9.com/-/spec/opensearchrss/1.0/}Query =
Element {http://www.w3.org/2005/Atom}entry = urn:uuid:b4f420fda52419f22007-03-22T03:00:00Z2007-03-22T03:00:00ZMeier, JohnMeier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30tel.search.ch1b4f420fda52419f2PersonMeierJohnMarienfeldstrasse928252SchlattTG+41526544230
Element {http://www.w3.org/2005/Atom}id = urn:uuid:b4f420fda52419f2
Element {http://www.w3.org/2005/Atom}updated = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}published = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}title = Meier, John
Element {http://www.w3.org/2005/Atom}content = Meier, John
Marienfeldstrasse 92
8252 Schlatt/TG
*052 654 42 30
Element {http://www.w3.org/2005/Atom}autor = tel.search.ch
Element {http://www.w3.org/2005/Atom}name = tel.search.ch
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://tel.search.ch/api/spec/result/1.0/}pos = 1
Element {http://tel.search.ch/api/spec/result/1.0/}id = b4f420fda52419f2
Element {http://tel.search.ch/api/spec/result/1.0/}type = Person
Element {http://tel.search.ch/api/spec/result/1.0/}name = Meier
Element {http://tel.search.ch/api/spec/result/1.0/}firstname = John
Element {http://tel.search.ch/api/spec/result/1.0/}occupation =
Element {http://tel.search.ch/api/spec/result/1.0/}street = Marienfeldstrasse
Element {http://tel.search.ch/api/spec/result/1.0/}streetno = 92
Element {http://tel.search.ch/api/spec/result/1.0/}zip = 8252
Element {http://tel.search.ch/api/spec/result/1.0/}city = Schlatt
Element {http://tel.search.ch/api/spec/result/1.0/}canton = TG
Element {http://tel.search.ch/api/spec/result/1.0/}phone = +41526544230
Element {http://www.w3.org/2005/Atom}entry = urn:uuid:c8c043412a3ce5262007-03-22T03:00:00Z2007-03-22T03:00:00ZJohn Meier IT-ConsultingJohn Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92tel.search.ch2c8c043402a3ce526OrganisationJohn Meier IT ConsultingYour Personal IT-ConsultantUnterdorfstrasse224143DornachSOSoftware GrosshandelSoftware & Consulting+41617236292+41617236393+41763341010john.meier@mymail.comhttp://www.johnmeierconsult.com
Element {http://www.w3.org/2005/Atom}id = urn:uuid:c8c043412a3ce526
Element {http://www.w3.org/2005/Atom}updated = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}published = 2007-03-22T03:00:00Z
Element {http://www.w3.org/2005/Atom}title = John Meier IT-Consulting
Element {http://www.w3.org/2005/Atom}content = John Meier IT-Consulting
Unterdorfstrasse 22
4143 Dornach/SO
061 723 62 92
Element {http://www.w3.org/2005/Atom}autor = tel.search.ch
Element {http://www.w3.org/2005/Atom}name = tel.search.ch
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://www.w3.org/2005/Atom}link =
Element {http://tel.search.ch/api/spec/result/1.0/}pos = 2
Element {http://tel.search.ch/api/spec/result/1.0/}id = c8c043402a3ce526
Element {http://tel.search.ch/api/spec/result/1.0/}type = Organisation
Element {http://tel.search.ch/api/spec/result/1.0/}name = John Meier IT Consulting
Element {http://tel.search.ch/api/spec/result/1.0/}occupation = Your Personal IT-Consultant
Element {http://tel.search.ch/api/spec/result/1.0/}street = Unterdorfstrasse
Element {http://tel.search.ch/api/spec/result/1.0/}streetno = 22
Element {http://tel.search.ch/api/spec/result/1.0/}zip = 4143
Element {http://tel.search.ch/api/spec/result/1.0/}city = Dornach
Element {http://tel.search.ch/api/spec/result/1.0/}canton = SO
Element {http://tel.search.ch/api/spec/result/1.0/}category = Software Grosshandel
Element {http://tel.search.ch/api/spec/result/1.0/}category = Software & Consulting
Element {http://tel.search.ch/api/spec/result/1.0/}phone = +41617236292
Element {http://tel.search.ch/api/spec/result/1.0/}extra = +41617236393
Element {http://tel.search.ch/api/spec/result/1.0/}extra = +41763341010
Element {http://tel.search.ch/api/spec/result/1.0/}extra = john.meier@mymail.com
Element {http://tel.search.ch/api/spec/result/1.0/}extra = http://www.johnmeierconsult.com
我真的很迷茫,希望得到任何帮助。
您正在返回 null。我修改了代码来处理命名空间 a remove "Value" ,它在 item 为空时给出异常。而是转换为 string
sing System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication94
{
class Program
{
static void Main(string[] args)
{
var url = "https://tel.search.ch/examples/api-response.xml";
var doc = new XDocument();
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
doc = XDocument.Load(resp.GetResponseStream());
}
XNamespace ns = doc.Root.GetDefaultNamespace();
XNamespace telNs = doc.Root.GetNamespaceOfPrefix("tel");
// Format Data
var query = from el in doc.Descendants(ns + "entry")
select new
{
Position = (string)el.Element(telNs + "pos"),
Id = (string)el.Element(telNs + "id"),
Type = (string)el.Element(telNs + "type"),
Name = (string)el.Element(telNs + "name"),
Firstname = (string)el.Element(telNs + "firstname"),
Occupation = (string)el.Element(telNs + "occupation"),
Street = (string)el.Element(telNs + "street"),
StreetNo = (string)el.Element(telNs + "streetno"),
Zip = (string)el.Element(telNs + "zip"),
City = (string)el.Element(telNs + "city"),
Canton = (string)el.Element(telNs + "canton"),
Phone = (string)el.Element(telNs + "phone")
email = el.Elements(telNs + "extra").Where(x => (string)x.Attribute("type") == "email").Select(x => (string)x).ToList() //use FirstOrDefault() if you only want one.
};
var list = query.ToList();
}
}
}