Как разобрать строку XMLDocument InnerText / Inner XML - PullRequest
0 голосов
/ 26 февраля 2020

У меня есть ответ XMLDocument.InnerText, который выглядит следующим образом

<result errorCode="0"><return><candidate><id>805</id><username /><forename>PROADMIN</forename><surname>ZZDEMO</surname><gender>U</gender><dob>01/01/1915</dob><candidateRef>037d23e4-fd0d-42d8-8858-e8de4c91c52c</candidateRef><uln /></candidate><exam><id>993</id><examState>4</examState><examId>1531</examId><examName>PDEM1</examName><duration>60</duration><totalDuration>60</totalDuration><autoSubmitDate>2020-02-25T23:59:00</autoSubmitDate><requiresSecureClient>1</requiresSecureClient><advanceDownload>0</advanceDownload><requiresConfirmationCheck>1</requiresConfirmationCheck><confirmationText>By ticking this box you confirm your details are correct and you accept the awarding organisation's code of conduct.</confirmationText><language>en        </language><certifiedAccessible>0</certifiedAccessible><examDeliverySystemStyleType>delivery_flat</examDeliverySystemStyleType><styleprofileId>8</styleprofileId><itemBankStyleProfileId>239</itemBankStyleProfileId><itemBankStyleProfileVersion>6</itemBankStyleProfileVersion><invigilated>0</invigilated><pinNumber>VJWRWG</pinNumber><startDateAndTime>Feb 25 2020 12:00AM</startDateAndTime><certifiedForTablet>0</certifiedForTablet><candidateDetailsTime>0</candidateDetailsTime><ndaTime>0</ndaTime><showCandidateDetailsPage>1</showCandidateDetailsPage><enableLogging>0</enableLogging><candidateBreakExtraDurationType>0</candidateBreakExtraDurationType><candidateBreakExtraDuration>0</candidateBreakExtraDuration><numberOfExtraBreaksPerSection>0</numberOfExtraBreaksPerSection><timeExtensionMin>0</timeExtensionMin><autoLaunchPdfViewer>0</autoLaunchPdfViewer><enableSearchInPdfViewer>1</enableSearchInPdfViewer><markingProgressMode>1</markingProgressMode><markingProgressVisible>1</markingProgressVisible><requiresBYODMode>0</requiresBYODMode><isHtmlCompatible>1</isHtmlCompatible><showAudioRecordSettings>0</showAudioRecordSettings></exam><token>4Oh9Av5ln4Gm7zyhzTHUecdy</token><centre><centreName>9222</centreName><centreCode>9222</centreCode><centreAddressLine1></centreAddressLine1><centreAddressLine2></centreAddressLine2><centreTown></centreTown><centreCounty>Unknown</centreCounty><centreCountry>Unknown</centreCountry><centrePostcode></centrePostcode></centre><styleProfile><providerLogoColourID>8CF11B73-D04B-4FC7-BA8E-26840D0466E3.png</providerLogoColourID><providerLogoMonochromeID>A5705F2E-8AB8-44EB-8575-43180FE1A2FA.png</providerLogoMonochromeID><clientLogoColourID></clientLogoColourID><clientLogoMonochromeID></clientLogoMonochromeID><deliveryWindowPosition>Centre</deliveryWindowPosition><headerFooterColourBackground>3D505A</headerFooterColourBackground><headerFooterColourText>FFFFFF</headerFooterColourText><finishButtonColourBackground>F7D78C</finishButtonColourBackground><finishButtonColourText>3D505A</finishButtonColourText><primaryButtonColourBackground>2B9ED8</primaryButtonColourBackground><primaryButtonColourText>FFFFFF</primaryButtonColourText><secondaryButtonColourBackground>3D505A</secondaryButtonColourBackground><secondaryButtonColourText>FFFFFF</secondaryButtonColourText><candidateFirstNameEnable>True</candidateFirstNameEnable><candidateLastNameEnable>True</candidateLastNameEnable><candidateDOBEnable>False</candidateDOBEnable><candidateGenderEnable>False</candidateGenderEnable><candidateReferenceEnable>False</candidateReferenceEnable><finishButtonShown>True</finishButtonShown><flagButtonShown>True</flagButtonShown><preferencesButtonShown>True</preferencesButtonShown><sectionReviewButtonShown>True</sectionReviewButtonShown><sectionInformationShown>True</sectionInformationShown><itemSetNumberingEnabled>False</itemSetNumberingEnabled><itemSetHeaderShown>False</itemSetHeaderShown><textForItemSetName></textForItemSetName><textForItemName></textForItemName><questionTitleDisplayMode>Name</questionTitleDisplayMode><warningIntervalOne>30</warningIntervalOne><warningIntervalTwo>15</warningIntervalTwo><warningIntervalThree>5</warningIntervalThree><warningDuration>30</warningDuration><correctItemsEnable>True</correctItemsEnable><incorrectItemsEnable>True</incorrectItemsEnable><unattemptedItemsEnable>True</unattemptedItemsEnable><candidateResponseEnable>True</candidateResponseEnable><correctAnswersEnable>True</correctAnswersEnable><candidateFeedbackEnable>True</candidateFeedbackEnable><questionInformationShown>True</questionInformationShown><introductionButtonShown>True</introductionButtonShown><sectionDetailShown>True</sectionDetailShown><nextButtonShown>True</nextButtonShown></styleProfile></return></result>

Ответ XMLDocument.Inner XML выглядит следующим образом

<?xml version="1.0" encoding="utf-8"?><string xmlns="">&lt;result errorCode="0"&gt;&lt;return&gt;&lt;candidate&gt;&lt;id&gt;805&lt;/id&gt;&lt;username /&gt;&lt;forename&gt;PROADMIN&lt;/forename&gt;&lt;surname&gt;ZZDEMO&lt;/surname&gt;&lt;gender&gt;U&lt;/gender&gt;&lt;dob&gt;01/01/1915&lt;/dob&gt;&lt;candidateRef&gt;037d23e4-fd0d-42d8-8858-e8de4c91c52c&lt;/candidateRef&gt;&lt;uln /&gt;&lt;/candidate&gt;&lt;exam&gt;&lt;id&gt;993&lt;/id&gt;&lt;examState&gt;4&lt;/examState&gt;&lt;examId&gt;1531&lt;/examId&gt;&lt;examName&gt;PDEM1&lt;/examName&gt;&lt;duration&gt;60&lt;/duration&gt;&lt;totalDuration&gt;60&lt;/totalDuration&gt;&lt;autoSubmitDate&gt;2020-02-25T23:59:00&lt;/autoSubmitDate&gt;&lt;requiresSecureClient&gt;1&lt;/requiresSecureClient&gt;&lt;advanceDownload&gt;0&lt;/advanceDownload&gt;&lt;requiresConfirmationCheck&gt;1&lt;/requiresConfirmationCheck&gt;&lt;confirmationText&gt;By ticking this box you confirm your details are correct and you accept the awarding organisation's code of conduct.&lt;/confirmationText&gt;&lt;language&gt;en        &lt;/language&gt;&lt;certifiedAccessible&gt;0&lt;/certifiedAccessible&gt;&lt;examDeliverySystemStyleType&gt;delivery_flat&lt;/examDeliverySystemStyleType&gt;&lt;styleprofileId&gt;8&lt;/styleprofileId&gt;&lt;itemBankStyleProfileId&gt;239&lt;/itemBankStyleProfileId&gt;&lt;itemBankStyleProfileVersion&gt;6&lt;/itemBankStyleProfileVersion&gt;&lt;invigilated&gt;0&lt;/invigilated&gt;&lt;pinNumber&gt;VJWRWG&lt;/pinNumber&gt;&lt;startDateAndTime&gt;Feb 25 2020 12:00AM&lt;/startDateAndTime&gt;&lt;certifiedForTablet&gt;0&lt;/certifiedForTablet&gt;&lt;candidateDetailsTime&gt;0&lt;/candidateDetailsTime&gt;&lt;ndaTime&gt;0&lt;/ndaTime&gt;&lt;showCandidateDetailsPage&gt;1&lt;/showCandidateDetailsPage&gt;&lt;enableLogging&gt;0&lt;/enableLogging&gt;&lt;candidateBreakExtraDurationType&gt;0&lt;/candidateBreakExtraDurationType&gt;&lt;candidateBreakExtraDuration&gt;0&lt;/candidateBreakExtraDuration&gt;&lt;numberOfExtraBreaksPerSection&gt;0&lt;/numberOfExtraBreaksPerSection&gt;&lt;timeExtensionMin&gt;0&lt;/timeExtensionMin&gt;&lt;autoLaunchPdfViewer&gt;0&lt;/autoLaunchPdfViewer&gt;&lt;enableSearchInPdfViewer&gt;1&lt;/enableSearchInPdfViewer&gt;&lt;markingProgressMode&gt;1&lt;/markingProgressMode&gt;&lt;markingProgressVisible&gt;1&lt;/markingProgressVisible&gt;&lt;requiresBYODMode&gt;0&lt;/requiresBYODMode&gt;&lt;isHtmlCompatible&gt;1&lt;/isHtmlCompatible&gt;&lt;showAudioRecordSettings&gt;0&lt;/showAudioRecordSettings&gt;&lt;/exam&gt;&lt;token&gt;4Oh9Av5ln4Gm7zyhzTHUecdy&lt;/token&gt;&lt;centre&gt;&lt;centreName&gt;9222&lt;/centreName&gt;&lt;centreCode&gt;9222&lt;/centreCode&gt;&lt;centreAddressLine1&gt;&lt;/centreAddressLine1&gt;&lt;centreAddressLine2&gt;&lt;/centreAddressLine2&gt;&lt;centreTown&gt;&lt;/centreTown&gt;&lt;centreCounty&gt;Unknown&lt;/centreCounty&gt;&lt;centreCountry&gt;Unknown&lt;/centreCountry&gt;&lt;centrePostcode&gt;&lt;/centrePostcode&gt;&lt;/centre&gt;&lt;styleProfile&gt;&lt;providerLogoColourID&gt;8CF11B73-D04B-4FC7-BA8E-26840D0466E3.png&lt;/providerLogoColourID&gt;&lt;providerLogoMonochromeID&gt;A5705F2E-8AB8-44EB-8575-43180FE1A2FA.png&lt;/providerLogoMonochromeID&gt;&lt;clientLogoColourID&gt;&lt;/clientLogoColourID&gt;&lt;clientLogoMonochromeID&gt;&lt;/clientLogoMonochromeID&gt;&lt;deliveryWindowPosition&gt;Centre&lt;/deliveryWindowPosition&gt;&lt;headerFooterColourBackground&gt;3D505A&lt;/headerFooterColourBackground&gt;&lt;headerFooterColourText&gt;FFFFFF&lt;/headerFooterColourText&gt;&lt;finishButtonColourBackground&gt;F7D78C&lt;/finishButtonColourBackground&gt;&lt;finishButtonColourText&gt;3D505A&lt;/finishButtonColourText&gt;&lt;primaryButtonColourBackground&gt;2B9ED8&lt;/primaryButtonColourBackground&gt;&lt;primaryButtonColourText&gt;FFFFFF&lt;/primaryButtonColourText&gt;&lt;secondaryButtonColourBackground&gt;3D505A&lt;/secondaryButtonColourBackground&gt;&lt;secondaryButtonColourText&gt;FFFFFF&lt;/secondaryButtonColourText&gt;&lt;candidateFirstNameEnable&gt;True&lt;/candidateFirstNameEnable&gt;&lt;candidateLastNameEnable&gt;True&lt;/candidateLastNameEnable&gt;&lt;candidateDOBEnable&gt;False&lt;/candidateDOBEnable&gt;&lt;candidateGenderEnable&gt;False&lt;/candidateGenderEnable&gt;&lt;candidateReferenceEnable&gt;False&lt;/candidateReferenceEnable&gt;&lt;finishButtonShown&gt;True&lt;/finishButtonShown&gt;&lt;flagButtonShown&gt;True&lt;/flagButtonShown&gt;&lt;preferencesButtonShown&gt;True&lt;/preferencesButtonShown&gt;&lt;sectionReviewButtonShown&gt;True&lt;/sectionReviewButtonShown&gt;&lt;sectionInformationShown&gt;True&lt;/sectionInformationShown&gt;&lt;itemSetNumberingEnabled&gt;False&lt;/itemSetNumberingEnabled&gt;&lt;itemSetHeaderShown&gt;False&lt;/itemSetHeaderShown&gt;&lt;textForItemSetName&gt;&lt;/textForItemSetName&gt;&lt;textForItemName&gt;&lt;/textForItemName&gt;&lt;questionTitleDisplayMode&gt;Name&lt;/questionTitleDisplayMode&gt;&lt;warningIntervalOne&gt;30&lt;/warningIntervalOne&gt;&lt;warningIntervalTwo&gt;15&lt;/warningIntervalTwo&gt;&lt;warningIntervalThree&gt;5&lt;/warningIntervalThree&gt;&lt;warningDuration&gt;30&lt;/warningDuration&gt;&lt;correctItemsEnable&gt;True&lt;/correctItemsEnable&gt;&lt;incorrectItemsEnable&gt;True&lt;/incorrectItemsEnable&gt;&lt;unattemptedItemsEnable&gt;True&lt;/unattemptedItemsEnable&gt;&lt;candidateResponseEnable&gt;True&lt;/candidateResponseEnable&gt;&lt;correctAnswersEnable&gt;True&lt;/correctAnswersEnable&gt;&lt;candidateFeedbackEnable&gt;True&lt;/candidateFeedbackEnable&gt;&lt;questionInformationShown&gt;True&lt;/questionInformationShown&gt;&lt;introductionButtonShown&gt;True&lt;/introductionButtonShown&gt;&lt;sectionDetailShown&gt;True&lt;/sectionDetailShown&gt;&lt;nextButtonShown&gt;True&lt;/nextButtonShown&gt;&lt;/styleProfile&gt;&lt;/return&gt;&lt;/result&gt;</string>

Я специально пытался получить значения для токена и идентификатора экзамена, и вот что я пробовал:

var token = document.GetElementsByTagName("token");
var exam = document.GetElementsByTagName("examId");

Оба из них показывают пустой XMLNodeList. Относительно плохо знаком с парсингом XML, что я делаю не так?

1 Ответ

1 голос
/ 26 февраля 2020

У меня нет проблем с кодом, который вы показали. В частности, я скопировал ваш первый фрагмент в файл с именем document.xml, а затем скомпилировал и запустил следующую программу:

using System;
using System.Xml;

public class Program
{
  public static void Main(string[] args)
  {
    XmlDocument document = new XmlDocument();

    document.Load("document.xml");

    var tokens = document.GetElementsByTagName("token");

    foreach(XmlElement token in tokens)
    {
      Console.WriteLine(token.InnerXml);
    }
  }
}

. При запуске выдает ожидаемый результат:

4Oh9Av5ln4Gm7zyhzTHUecdy

Так что, что бы ни случилось с вашей программой, это не в коде, который вы здесь показали.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...