I have to create an XML file dynamically based on the user input.
Here is what I came up with and I am struck up with two issues.
- if there is a collection of same element (MaxOccurs = 10) (For example if the user entered 4 accounts then how should my code be)
- If there is a choice option. Based on the element chosen the child elements should change.
Somebody please help me.
Thanks in advance
BB
My code :
XElement req =
new XElement("order",
new XElement("client",
new XAttribute("id", clientId),
new XElement("quoteback",
new XAttribute ("name",quotebackname)
)
),
new XElement("accounting",
new XElement("account"),
new XElement("special_billing_id")
),
new XElement("products",
new XElement(
**productChoiceType**,
***** HERE THE ELEMENTS WILL CHAGE BASED ON **productChoiceType**
)
)
)
);
LINQ comes in handy for things like this:
Use an XmlWriter object instead, at least imo it is easier to do the sort of things you want. You can then structure it something like:
Or create a class for each type that you want to convert to XML and use the XmlSerializer.