为什么我的XmlReader会返回一个空字符串?

wil*_*lem 4 .net xml xmlreader

我需要从XmlReader获取完整的Xml字符串(长篇故事).但是,在此示例代码中,最终变量theXmlString保持为空.为什么不分配Xml字符串?

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();
Run Code Online (Sandbox Code Playgroud)

rrr*_*rrr 8

只需要先开始阅读,Read()然后使用移动到节点然后ReadOuterXml()实际读取值.

var reader = XmlReader.Create(new StringReader(xmlConfig));
reader.Read();
string theXmlString = reader.ReadOuterXml();
Run Code Online (Sandbox Code Playgroud)

或者你也应该可以使用reader.MoveToContent();.

  • 就如此容易.谢谢rRrRrRr (2认同)