开发者

using an expression inside XContainer()

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)开发者_JAVA技巧));
    xml.Save(pfileLocation);

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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜