如何在wpf中读取xml [英] how to read xml in wpf

查看:67
本文介绍了如何在wpf中读取xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个xml

i have a xml

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">

   <S:Body>
      <ns2:getApplicantResponse xmlns:ns2="http://ws.mcint/">
         <return>
            <mcNo>1000001404</mcNo>
            <firstNameAr>???</firstNameAr>
            <firstNameEn>RAJ</firstNameEn>
            <middleNameAr>??????</middleNameAr>
            <middleNameEn>NARAYAN</middleNameEn>
            <lastNameAr>????</lastNameAr>
            <lastNameEn>KHATWE</lastNameEn>
            <sex>M</sex>
            <dateOfBirth>1969-05-17T00:00:00+03:00</dateOfBirth>
            <fatherNameAr>??????</fatherNameAr>
            <fatherNameEn>NARAYAN</fatherNameEn>
            <motherNameAr>NARAYAN</motherNameAr>
            <motherNameEn>??????</motherNameEn>
            <countryCode>524</countryCode>
            <episodes>
               <mcEpisodesPK>
                  <episodeNo>1</episodeNo>
                  <mcNo>1000001404</mcNo>
               </mcEpisodesPK>
               <visaType>04</visaType>
               <visaNo>032005025647</visaNo>
               <passportNo>2095420</passportNo>
               <maritalStatus>U</maritalStatus>
               <pregnancyIndicator>N</pregnancyIndicator>
               <xrayRequired>Y</xrayRequired>
               <occupationCode>540</occupationCode>
               <sponsorCode>OTH</sponsorCode>
               <applicantTests>
                  <groupCode>HIV</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>1</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>1</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>401001374</specimenNo>
                  <testCode>ANTI-HIV</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>1</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>1</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>HIV</groupCode>
                  <testCode>ANTI-HIV</testCode>
                  <specimenNo>401001374</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>XG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>1</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>2</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>0</specimenNo>
                  <testCode>CXR</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>1</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>2</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>XG1</groupCode>
                  <testCode>CXR</testCode>
                  <specimenNo>0</specimenNo>
               </applicantTests>
               <applicationDetails>
                  <mcApplicationDetailsPK>
                     <episodeNo>1</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <typeCode>WRK</typeCode>
                  </mcApplicationDetailsPK>
                  <mcApplicationDetailsPK>
                     <episodeNo>1</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <typeCode>WRK</typeCode>
                  </mcApplicationDetailsPK>
               </applicationDetails>
            </episodes>
            <episodes>
               <mcEpisodesPK>
                  <episodeNo>2</episodeNo>
                  <mcNo>1000001404</mcNo>
               </mcEpisodesPK>
               <visaType>04</visaType>
               <visaNo>26581802115</visaNo>
               <passportNo>2091187</passportNo>
               <maritalStatus>U</maritalStatus>
               <pregnancyIndicator>N</pregnancyIndicator>
               <xrayRequired>Y</xrayRequired>
               <occupationCode>574</occupationCode>
               <sponsorCode>OTH</sponsorCode>
               <applicantTests>
                  <groupCode>LG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>1</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>101327859</specimenNo>
                  <testCode>ANTI-HCV</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>1</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>LG1</groupCode>
                  <testCode>ANTI-HCV</testCode>
                  <specimenNo>101327859</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>LG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>2</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>101327859</specimenNo>
                  <testCode>ANTI-HIV</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>2</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>LG1</groupCode>
                  <testCode>ANTI-HIV</testCode>
                  <specimenNo>101327859</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>XG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>3</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>0</specimenNo>
                  <testCode>CXR</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>3</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>XG1</groupCode>
                  <testCode>CXR</testCode>
                  <specimenNo>0</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>LG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>4</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>101327859</specimenNo>
                  <testCode>HBSAG</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>4</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>LG1</groupCode>
                  <testCode>HBSAG</testCode>
                  <specimenNo>101327859</specimenNo>
               </applicantTests>
               <applicationDetails>
                  <mcApplicationDetailsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <typeCode>RFL</typeCode>
                  </mcApplicationDetailsPK>
                  <mcApplicationDetailsPK>
                     <episodeNo>2</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <typeCode>RFL</typeCode>
                  </mcApplicationDetailsPK>
               </applicationDetails>
            </episodes>
            <episodes>
               <mcEpisodesPK>
                  <episodeNo>3</episodeNo>
                  <mcNo>1000001404</mcNo>
               </mcEpisodesPK>
               <visaType>04</visaType>
               <visaNo>29114405896</visaNo>
               <passportNo>29114405896</passportNo>
               <maritalStatus>U</maritalStatus>
               <pregnancyIndicator>N</pregnancyIndicator>
               <xrayRequired>Y</xrayRequired>
               <occupationCode>757</occupationCode>
               <sponsorCode>OTH</sponsorCode>
               <applicantTests>
                  <groupCode>PRF</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>1</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>651045862</specimenNo>
                  <testCode>ANTI-HBCT</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>1</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>PRF</groupCode>
                  <testCode>ANTI-HBCT</testCode>
                  <specimenNo>651045862</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>PRF</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>2</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>651045862</specimenNo>
                  <testCode>ANTI-HBS</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>2</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>PRF</groupCode>
                  <testCode>ANTI-HBS</testCode>
                  <specimenNo>651045862</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>LG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>3</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>101860252</specimenNo>
                  <testCode>ANTI-HCV</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>3</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>LG1</groupCode>
                  <testCode>ANTI-HCV</testCode>
                  <specimenNo>101860252</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>LG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>4</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>101860252</specimenNo>
                  <testCode>ANTI-HIV</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>4</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>LG1</groupCode>
                  <testCode>ANTI-HIV</testCode>
                  <specimenNo>101860252</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>XG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>5</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>0</specimenNo>
                  <testCode>CXR</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>5</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>XG1</groupCode>
                  <testCode>CXR</testCode>
                  <specimenNo>0</specimenNo>
               </applicantTests>
               <applicantTests>
                  <groupCode>LG1</groupCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>6</testNo>
                  </mcApplicantTestsPK>
                  <specimenNo>101860252</specimenNo>
                  <testCode>HBSAG</testCode>
                  <mcApplicantTestsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <testNo>6</testNo>
                  </mcApplicantTestsPK>
                  <groupCode>LG1</groupCode>
                  <testCode>HBSAG</testCode>
                  <specimenNo>101860252</specimenNo>
               </applicantTests>
               <applicationDetails>
                  <mcApplicationDetailsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <typeCode>BBL</typeCode>
                  </mcApplicationDetailsPK>
                  <mcApplicationDetailsPK>
                     <episodeNo>3</episodeNo>
                     <mcNo>1000001404</mcNo>
                     <typeCode>BBL</typeCode>
                  </mcApplicationDetailsPK>
               </applicationDetails>
            </episodes>
            <contact>
               <contact>5919200</contact>
               <mcApplicantContactsPK>
                  <contactCode>WK</contactCode>
                  <mcNo>1000001404</mcNo>
               </mcApplicantContactsPK>
               <remarks>5919200</remarks>
               <mcApplicantContactsPK>
                  <contactCode>WK</contactCode>
                  <mcNo>1000001404</mcNo>
               </mcApplicantContactsPK>
               <contact>5919200</contact>
               <remarks>5919200</remarks>
            </contact>
         </return>
      </ns2:getApplicantResponse>
   </S:Body>
</S:Envelope>







i want read each tag from this xml(eg;mcno).how i read this xml?




i want read each tag from this xml(eg;mcno).how i read this xml?

推荐答案

Maybe you don’t need to parse XML directly but need to use SOAP System.Runtime.Serialization.Formatters.Soap.SoapFormatter:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter%28v=vs.110%29.aspx[^].



It all depends on your purpose. To parse XML from scratch, .NET FCL provides at least three different ways. Please see my short overview of them below:

Maybe you don't need to parse XML directly but need to use SOAP System.Runtime.Serialization.Formatters.Soap.SoapFormatter:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter%28v=vs.110%29.aspx[^].

It all depends on your purpose. To parse XML from scratch, .NET FCL provides at least three different ways. Please see my short overview of them below:
  1. Use System.Xml.XmlDocument class.它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].









And it has nothing to do with WPF. You need to embrace the idea of separation of concerns: http://en.wikipedia.org/wiki/Separation_of_concerns[^].



Good luck,

—SA


various way you can achieve that. You can use recursive way to navigate all xml element using xlinq.



The following link will show the way recursive algorithm implement.



http://www.java2s.com/Tutorial/CSharp/0540__XML/LoopThroughXmlDocumentRecursively.htm[^]
various way you can achieve that. You can use recursive way to navigate all xml element using xlinq.

The following link will show the way recursive algorithm implement.

http://www.java2s.com/Tutorial/CSharp/0540__XML/LoopThroughXmlDocumentRecursively.htm[^]


using System.Xml.XmlDocument.SelectNodes (MSDN Library) you can get the list of all nodes \"mcNo\"



using System.Xml.XmlDocument.SelectNodes (MSDN Library) you can get the list of all nodes "mcNo"

XmlDocument xml = new XmlDocument();
xml.Load(fileName);
XmlNodeList nodes = xml.SelectNodes("//mcNo"); // double '/' to get nodes at any level (XPath syntax)


这篇关于如何在wpf中读取xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆