如何解析xml文件以获取字段?(How to parse an xml file to get the fields?)

编程入门 行业动态 更新时间:2024-10-27 00:25:03
如何解析xml文件以获取字段?(How to parse an xml file to get the fields?)

我有一个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&amp;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'有一个无效的令牌

I have an xml file and I need to parse it to get the 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&amp;wt.mc_id=66ttet</d:Url> </m:properties> </content> </entry> </m:inline> </entry>

I am using the piece of code to generate the information -

// 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); }

In the above program I want to get the information as "//de:entry/de:link/de:m:inline/de:feed/de:id" i.e trying to extract the information under but the program does not recognize "/de:m:inline" as the right format and generating an error stating "incorrect format" , I wish to parse the information to get the data as specified but do not know how to generate the right format. New to C# please help me with the following.

Sample of the code that fails:

xmlFile.SelectNodes("//de:link/de:m:inline", xmlnm);

Error:

'//de:m:inline' has an invalid token

最满意答案

添加名称空间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); } }

Add both namespaces de and 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); } }

更多推荐

本文发布于:2023-04-27 22:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   文件   xml   fields   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!