1

I am able to get my results, but seems like there is a better way to do it.

        doc.Element("XML").SetAttributeValue("Type", "ListBuilder");
        doc.Element("XML")
           .Element("Order").SetAttributeValue("No", 425654);
        doc.Element("XML")
           .Element("Order").SetAttributeValue("DispDate", today);
        doc.Element("XML")
           .Element("Order").SetAttributeValue("Basket", 3536);

enter image description here

2 Answers 2

2

You could do the same by adding XAttribute items to the elements. This leaves you with more options in general as these could be dynamically generated if you needed to.

doc.Element("XML").Add(new XAttribute("Type", "ListBuilder"));
doc.Element("XML").Element("Order").Add(new[]
{
    new XAttribute("No", 425654),
    new XAttribute("DispDate", today),
    new XAttribute("Basket", 3536),
});
Sign up to request clarification or add additional context in comments.

1 Comment

I completely forgot with Add method you can add any kind of content. I totally support this answer.
1

Maybe saving your element references in variables to not search them every time you need to add an attribute to them:

var xml= doc.Element("XML");
xml.SetAttributeValue("Type", "ListBuilder");

var order=xml.Element("Order");
order.SetAttributeValue("No", 425654);
//...

1 Comment

That is certainly better than what I had. Thank you

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.