tags:

views:

13

answers:

1

I have two xml files but one file will contain an extra field. Ideally I would like to add an if statement withing the XElement but I dont think its possible.

Obviously this is horribly wrong, its just an example to give you some idea what I would like to do:

    XElement xml = XElement.Load(pfileLocation);

    xml.Add(new XElement("Something",
        new XAttribute("widgit", pwidgitID),

        if (pfileLocation == "file1.xml")
            new XElement("Foo", pfoo),

        new XElement("Bar", pbar)));
    xml.Save(pfileLocation);

My guess is that I should pass it into an overloaded method?

+1  A: 

Have you tried using the ternary operator? I think this should work:

XElement xml = XElement.Load(pfileLocation);

xml.Add(new XElement("Something",
    new XAttribute("widgit", pwidgitID),

    (pfileLocation == "file1.xml")?
        new XElement("Foo", pfoo):null,
        new XElement("Bar", pbar)));

xml.Save(pfileLocation);
awshepard
Very Nice......
Leroy Jenkins