如何解析 XMLDocument InnerText/InnerXML 字符串
How to parse XMLDocument InnerText/InnerXML string
我有一个 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.InnerXML 响应如下所示
<?xml version="1.0" encoding="utf-8"?><string xmlns=""><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></string>
我一直在专门尝试获取令牌和考试 ID 的值,这就是我尝试过的方法:
var token = document.GetElementsByTagName("token");
var exam = document.GetElementsByTagName("examId");
这两个都显示一个空的 XMLNodeList。对解析 XML 比较陌生,我做错了什么?
我对您显示的代码没有任何问题。特别是,我将您的第一个片段复制到一个名为 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
因此,无论您的程序有什么问题,都不会出现在您在此处显示的代码中。
我有一个 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.InnerXML 响应如下所示
<?xml version="1.0" encoding="utf-8"?><string xmlns=""><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></string>
我一直在专门尝试获取令牌和考试 ID 的值,这就是我尝试过的方法:
var token = document.GetElementsByTagName("token");
var exam = document.GetElementsByTagName("examId");
这两个都显示一个空的 XMLNodeList。对解析 XML 比较陌生,我做错了什么?
我对您显示的代码没有任何问题。特别是,我将您的第一个片段复制到一个名为 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
因此,无论您的程序有什么问题,都不会出现在您在此处显示的代码中。