2012-03-01 116 views
3

我需要從XmlReader(長篇故事)中獲取完整的Xml字符串。在這個示例代碼中,最後一個變量theXmlString保持爲空。爲什麼它沒有被賦予Xml字符串?爲什麼我的XmlReader會返回一個空字符串?

string xmlConfig = @"<pdfMappings> 
         <pdfFile formTypeEnum=""Int_UT_Additional_Investment_Form_Ind_And_LE_direct""> 
          <perspective ngiAdminPerspectiveName=""Investor""> 
           <fieldMapping fieldName=""topmostsubform[0].Page2[0].first_names[0]"" mapTo=""CurrentInvolvedParty.FirstName""></fieldMapping> 
           <fieldMapping fieldName=""topmostsubform[0].Page2[0].surname[0]"" mapTo=""CurrentInvolvedParty.LastName""></fieldMapping> 
          </perspective> 
         </pdfFile> 
        </pdfMappings>"; 
var reader = XmlReader.Create(new StringReader(xmlConfig)); 

string theXmlString = reader.ReadOuterXml(); 

回答

7

剛需開始讀第一,使用Read()移動到節點然後ReadOuterXml()實際讀取的值。

var reader = XmlReader.Create(new StringReader(xmlConfig)); 
reader.Read(); 
string theXmlString = reader.ReadOuterXml(); 

另外,您還應該能夠使用reader.MoveToContent();

+2

就這麼簡單。謝謝rRrRrRr – willem 2012-03-01 12:13:07

相關問題