2014-01-17 2 views
0

나는 xml 파일을 가지고 있고 나는 정보를 생성하는 코드의 조각을 사용하고있는 정보 및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>[email protected]</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 "올바른 형식으로"잘못된 형식 "이라는 오류가 발생하면 지정된대로 데이터를 얻으려는 정보를 구문 분석하려고하지만 올바른 형식을 생성하는 방법을 알지 못합니다. rmat. C#을 처음 접 하시다니 다음을 도와주세요.

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

오류 :

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

+1

예외가 발생하면 줄을 게시하고 스택하고 정확한 오류 메시지를 게시하십시오. –

+0

'// de : entry/de : link/de : m : 인라인/de : feed/de : id'에 잘못된 토큰이 있습니다. – user2825395

+0

@ user3199528 질문 할 때 응답해야합니다. – PawanS

답변

1

de

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); 
     } 
    } 
+0

도움에 감사드립니다. C# 사용자가 처음으로 이러한 것들은 꽤 까다 롭습니다. – user2825395

0

m은 당신이 할 수있는 XML 파일에서 정보를 얻으려면 두 네임 스페이스를 추가 실패 코드의

샘플 XDocument를 사용하여 데이터를 전달한 다음 eac에서 반복합니다. h 요소 (노드)가 파일에 존재하고 내부 노드 또는 속성 등을 얻습니다. 다음과 같이 : 문자열 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; 
관련 문제