如何解析XML以获取c#中的数据? [英] how to parse XML to get the data in c#?
问题描述
我有一个xml文件,我想把信息写到csv文件。
I have an xml file and I want to the write the information on to the csv file.
我试图解析xml来获取字段和数据并写入到csv文件。到目前为止,我已经能够解析数据,以获得关于
'title'和'id'在节点'条目'下的信息,但我试图解析节点下的数据'','id'信息。
I am trying to parse the xml to get the fields and the data and write onto a csv file. So far I have been able to parse the data to get the information in relation to 'title' and 'id' under node 'entry' but I am trying to parse the data under node '', 'id' information.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://google.com/en-US/syndicate/" xmlns:d="http://schemas.google.com/ado/2007/08/dataservices" xmlns:m="http://schemas.giooglt.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Partners</title>
<id>http://googlre.com/en-US/syndicate/Partners</id>
<updated>2014-01-16T21:33:20Z</updated>
< link rel="self" title="Partners" href="Partners" />
<entry>
<id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
<title type="text">M55p; Co</title>
<summary type="text">cccc is a Certified Partner, reseller, and implementer of
Key industries we work with include:
• Financial services
• Professional services
• Media / publishing
By focusing on mid-market to enterprise clients,
</summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-11-22T15:00:46-08:00</updated>
<author>
<name>google chrome</name>
<uri>http://google.com/</uri>
<email>retee@gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4255')" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/('429')/Links</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Links" href="Partners('4ff')/Links" />
<entry>
<id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
<title type="text">
</title>
<updated>2014-01-16T21:33:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>pgooglrpartnerpage</d:Type>
<d:Description>google Partner Page</d:Description>
<d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet</d:Url>
</m:properties>
</content>
</entry>
</m:inline>
</entry>
<entry>
<id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
<title type="text">M55p; Co</title>
<summary type="text">cccc is a Certified Partner, reseller, and implementer of
Key industries we work with include:
• Financial services
• Professional services
• Media / publishing
By focusing on mid-market to enterprise clients,
</summary>
<published>2009-07-21T14:23:50-07:00</published>
<updated>2013-11-22T15:00:46-08:00</updated>
<author>
<name>google chrome</name>
<uri>http://google.com/</uri>
<email>retee@gmail.com</email>
</author>
<link rel="edit" title="Partner" href="Partners('4255')" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
<m:inline>
<feed>
<title type="text">Links</title>
<id>http://google.com/('429')/Links</id>
<updated>2014-01-16T21:33:20Z</updated>
<link rel="self" title="Links" href="Partners('4ff')/Links" />
<entry>
<id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
<title type="text">
</title>
<updated>2014-01-16T21:33:20Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Link" href="Links('partnerpage')" />
<category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Type>pgooglrpartnerpage</d:Type>
<d:Description>google Partner Page</d:Description>
<d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&wt.mc_id=66ttet</d:Url>
</m:properties>
</content>
</entry>
</m:inline>
</entry>
</feed>
条目将有所不同,但是我想提取信息, CSV文件为 -
The entries will be different but as one sees, I want to extract the information and write it onto a CSV file as -
标题,ID,PinpointId
// de:entry / de:title,// de:entry / de:title, de:entry / de:link / m:inline / de:feed / de:id
Title,Id,PinpointId //de:entry/de:title,//de:entry/de:title,//de:entry/de:link/m:inline/de:feed/de:id
。
用于获取信息的代码片段是:
The piece of code being used to get the information is :
// Alternate Method for getting the Fields from the XML file
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("C:/Users/Administrator/Downloads/direct.xml");
XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
xmlnm.AddNamespace("de","http://www.w3.org/2005/Atom");
xmlnm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
xmlnm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
ParseXML(xmlDocument, xmlnm);
Debug.WriteLine("\n---XML parsed---");
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
String path = "C:/Users/Administrator/Downloads/data.csv";
var w = new StreamWriter(path);
XmlNodeList nodes = xmlFile.SelectNodes("//de:entry", xmlnm);
foreach (XmlNode node in nodes)
{
string titl = node["title"].InnerText;
string ide = node["id"].InnerText;
//string cty = node["link/m:inline/de:feed/de:id"].InnerText;
Debug.WriteLine("Data :" + titl+"ID :"+ide);
}
}
The output of the program is -
Data :MIG & CoID :http://pinpoint.microsoft.com/en-US/syndicate/Partners('4295719419')
我试图解析数据。
string cty = node [link / m:inline / de:feed / de:id]。InnerText;
I am trying to parse the data like. string cty = node["link/m:inline/de:feed/de:id"].InnerText;
对象引用未设置为对象的实例。
But the error generated. "Object reference not set to an instance of an object."
xml有多个条目和类似的格式。新的C#如何获取信息并写入CSV文件。请帮助我。
The xml has got multiple entries and in similar format. New to C# how to get the information and write it on to the CSV file.Please help me.
推荐答案
首先,你的xml不好形成。它需要纠正。第二,我不确定你尝试使用查询link / m:inline / de:feed / de:id
First, your xml is not well formed. It will need to be corrected. Second, I am unsure exactly what node you are attempting to return with the query "link/m:inline/de:feed/de:id"
这样说,你的节点[link / m:inline / de:feed / de:id]
所以它不会被设置为你期望的XmlNode对象。这是因为XmlNode项目需要一个节点的名称,而不是一个xpath查询。如果要传递xpath查询,请使用SelectSingleNode。像这样:
With that said, your node["link/m:inline/de:feed/de:id"]
isn't valid, so it's not getting set to the XmlNode object you are expecting. That's because an XmlNode item expects the name of a node, not an xpath query. Use SelectSingleNode if you want to pass an xpath query. Something like this:
foreach (XmlNode node in nodes)
{
string titl = node["title"].InnerText;
string ide = node["id"].InnerText;
string cty;
var ctyNode = node.SelectSingleNode("link/m:inline/de:feed/de:id");
if (ctyNode != null)
{
cty = ctyNode.InnerText
}
Debug.WriteLine("Data :" + titl+"ID :"+ide);
}
这篇关于如何解析XML以获取c#中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!