Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would my XmlReader return an empty string?

I need to get the full Xml string from an XmlReader (long story). In this sample code though, the final variable, theXmlString, remains empty. Why does it not get assigned the Xml string?

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();
like image 434
willem Avatar asked Jan 17 '23 08:01

willem


1 Answers

Just need to start reading first, use Read() to move to the node then ReadOuterXml() to actually read the value.

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

Alternatively you should also be able to use reader.MoveToContent();.

like image 131
rrrr-o Avatar answered Feb 03 '23 21:02

rrrr-o