我有一个xml文件,我需要解析它以获取信息 -
<?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>
我正在使用这段代码生成信息 -
// 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");
ParseXML(xmlDocument, xmlnm);
Debug.WriteLine("\n---XML parsed---");
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
//XmlNodeList nodes = xmlFile.SelectNodes("//ns:entry/ns:updated| //ns:entry/ns:published | //ns:entry/ns:id ", xmlnm);
XmlNodeList nodes = xmlFile.SelectNodes("//de:entry/de:link/de:inline/de:feed/de:id ", xmlnm);
foreach (XmlNode node in nodes)
{
Debug.WriteLine(node.Name + " = " + node.InnerXml);
}
在上面的程序中,我希望获得的信息为“// de:entry / de:link / de:m:inline / de:feed / de:id”即尝试提取信息,但程序确实如此不承认“/ de:m:inline”作为正确的格式并生成错误,指出“格式不正确”,我希望解析信息以获取指定的数据,但不知道如何生成正确的格式。 C#的新用户请帮我解决以下问题。
失败的代码示例:
xmlFile.SelectNodes("//de:link/de:m:inline", xmlnm);
错误:
'// de:m:inline'的标记无效
答案 0 :(得分:1)
添加名称空间de
和m
public static void Main()
{
// 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");
**** add this too****
xmlnm.AddNamespace("m", "http://schemas.giooglt.com/ado/2007/08/dataservices/metadata");
ParseXML(xmlDocument, xmlnm);
Console.WriteLine("\n---XML parsed---");
Console.ReadKey();
}
public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
{
/// inline should be ""m"" not ""de""
XmlNodeList nodes = xmlFile.SelectNodes("//de:entry/de:link/m:inline/de:feed/de:id", xmlnm);
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Name + " = " + node.InnerXml);
}
}
答案 1 :(得分:0)
要从XML文件中获取信息,您可以使用XDocument将数据传递给它,然后迭代文件中存在的每个元素(节点)并获取其内部节点或属性等。 像这样 : string PdFileContent = string.Empty;
string FileContent = string.Empty;
using (StreamReader Reader = new StreamReader(**FilePath**, UTF8Encoding.UTF8))
{
FileContent = Reader.ReadToEnd();
}
XmlDocument XDoc = new XmlDocument();
XDoc.LoadXml(FileContent); //Load the file with XML strcuture
XDoc.SelectNodes("/**NodeNameToSelect**");
// Read inner text of the XDocument's Child Elements
string Result = XDoc["**ChildNodeName**"].InnerText;