1

I have this:

var doc = new XmlDocument();
doc.LoadXml("<XMLHERE/>"); //This returns void

How do I combine into one statement?

2
  • 1
    new Action(() => { var doc = new XmlDocument(); doc.LoadXml("XML"); }).Invoke(); Almost single statement )) Commented Jan 31, 2012 at 8:46
  • Another thing new Action<XmlDocument>((x) => x.Load("Your xml"))(new XmlDocument()); Commented Jan 31, 2012 at 8:50

6 Answers 6

5

Use static XDocument.Parse(string xml) which returns an XDocument object;

    XDocument doc = XDocument.Parse("<XMLHERE/>");
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer, if the user has the supporting .NET version (3.5 and above).
var foo = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes("<xml />"))); The overload of static member of XDocument Load that accepts string as a parameter expects URI
This is great but has to be from System.Xml unfortunately.
4

You can't, lest you lose the reference to the document (doc) and therefore find it to be inaccessible.

As other answers have began filtering in to contradict my assertion, what I will say is this. Of course, you can create a method to do 'the dirty work' for you, but then you're not really just turning this into a one-liner, you're just disjointing the creation (and not really saving unless you need to write this in hundreds of different places).

This might not be a bad thing in many situations, but defining a class with a single extension method to facilitate this seems ridiculous. Specifying it locally, within a class where this would be extensively utilised could be a different matter.

All in all, my answer still stands fundamentally, not considering the many contrived ways you might 'get around' this.

1 Comment

Thanks - as I suspected. Marked this as the answer but thanks for everyone's responses. Much appreciated.
2

this is probably your best (alternative to two lines) option...

public static XmlDocument MyLazyAssFunction(xml)
{
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    return doc;
}

then here is your single statment...

var doc = MyLazyAssFunction("<XMLHERE/>");

The point here being that your original two lines is a perfectly fine way of doing what you need to do.. and it is very readable as it stands too

4 Comments

LOL! Good name for the function, I must say! :D By the way, you forgot static.
I think the 'best' option would be to write two lines, rather than create a class for this sole purpose.
This will also cause a compile-time error: you must return doc.
@Mr.Disappointment: Not sure why I missed that out. Am I that unknowingly reliant on VS prompts :S
1

With just the XmlDocument API you can't do this in a single line and keep the XmlDocument reference. However you could write a helper API

public static class XmlUtils {
  public static XmlDocument CreateDocument(string xml) {
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    return doc;
  }
}

var doc = XmlUtils.CreateDocument("<XMLHERE/>");

Comments

1

You cannot. If defining the variable does not count as a statement, you can write:

XmlDocument doc;
(doc = new XmlDocument()).LoadXml("<XMLHERE/>");

Comments

1

You can write extension:

public static XmlDocument MyLoad(this XmlDocument doc, string xml)
{
    doc.LoadXml(xml);
    return doc;
}

Usage:

var doc = new XmlDocument().MyLoad(xml); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.