4

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.

  1. if there is a collection of same element (MaxOccurs = 10) (For example if the user entered 4 accounts then how should my code be)
  2. 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**           
                )
            )
        )
    );
egrunin
  • 24,650
  • 8
  • 50
  • 93
BumbleBee
  • 10,429
  • 20
  • 78
  • 123

3 Answers3

6

LINQ comes in handy for things like this:

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(productChoices.Single(pc => pc.ChoiceType == choiceType).Name, 
                    from p in products
                    where p.ChoiceType == choiceType
                    select new XElement(p.Name)
              )
          )
      );
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • StriplingWarrior thank you very much. Based on (p.Name) selected I have to add a complete different set of elements to "products". – BumbleBee Nov 16 '10 at 23:02
  • say if p.Name is CLUEAuto then i have to add parameter,pnc,usage Elements to "products". If p.Name is Mortgae then I have to add parameter,RiskAddress,CurrentAddress,PreviousAddress,Mortgage elements to Products – BumbleBee Nov 16 '10 at 23:10
  • new XElement("parameter),new XElement("pnc"),new XElement("usage", ClueAutoUsageEnum), – BumbleBee Nov 16 '10 at 23:11
  • 1
    @BumbleBee: Apply the same principles you saw me use: `...new XElement(p.Name, from t in thingRepository.GetByProductName(p.Name) select new Element(t.Whatever)` – StriplingWarrior Nov 16 '10 at 23:13
2

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:

XmlWriter w = XmlWriter.Create(outputStream);
w.WriteStartElement("order");

w.WriteStartElement("client");
w.WriteAttributeString("id", clientId);

// ...
w.WriteElementString("product", "1");
w.WriteElementString("product", "2");
w.WriteElementString("product", "3");
w.WriteElementString("product", "4");

// etc....

w.WriteEndElement(); // client

w.WriterEndElement(); // order
tyranid
  • 13,028
  • 1
  • 32
  • 34
0

Or create a class for each type that you want to convert to XML and use the XmlSerializer.

<XmlElement("order")> _
Public Class Order
    <XmlElement("accounting")> _
    Dim accounts As List(Of Account)
    ...
End Class

Dim xmlSer as New XmlSerialzer(GetType(Accounting))
xmlSer.Serialize(myXmlWriter, myObjInstance)
mellamokb
  • 56,094
  • 12
  • 110
  • 136