Saturday 15 May 2010

.net - Can combine XDocument.Load() and XDocument.parse() into one? -



.net - Can combine XDocument.Load() and XDocument.parse() into one? -

i have xdocument.load() loads xml file , xdocument.parse() loads xml string, can find way combined , load file or string dynamically ?

under assumption mean xdocument.parse() , not xdocument.prase(), believe below function should help in regard. check see if file exists first before loading it, if doesn't exist should xml string. in both cases cover ourselves seek grab , homecoming null.

to utilize below double check homecoming not null before continuing.

if(loadxml(xmlfile) == null) { //enter code fail load, go on if correct. }

the function:

private xdocument loadxml(string xmlfile) { //initialise new xdocument xdocument doc = new xdocument(); //check see if xmlfile exists file in os. if (file.exists(xmlfile)) { //try xdocument.load() seek { doc = xdocument.load(xmlfile); } grab (exception) { //if load did not succeed homecoming null homecoming null; } } else { //if not file seek parsing string. seek { doc = xdocument.parse(xmlfile); } grab (exception) { //if parse failed (i.e. string not xml format) create doc = null homecoming null; } } //return doc homecoming doc; }

.net linq-to-xml

No comments:

Post a Comment