1

I am a C# developer only with 1-year experience. I am facing an issue with creating complex xml request for a web service.

I have received the XSD file & the sample xml requests. Now I've to populate it with correct data and call web service. I 've a little experience in creating small xml structures. What I was doing is using string concatination /xml document object / xml writer . Those methodsare good with small structures but with bigger structure it's not easy to write each and every tags using above object models.

Please let me know the best way of creating complex xml structures from C# . Thanks.

C1rdec
  • 1,647
  • 1
  • 21
  • 46
  • 2
    try this: http://stackoverflow.com/questions/284324/how-can-i-build-xml-in-c – dr11 Apr 09 '15 at 20:32
  • 1
    There is not enough information here to help you. Please show the code you've created and examples of where it fails including any errors. – crthompson Apr 09 '15 at 20:32
  • I would suggest adding xml attributes to your classes, then using the XmlSerializer – MIke Apr 09 '15 at 20:34
  • @MikeN There is more than 30 nodes & also it has lots of child nodes. Still i didnt create the class to serialize because creating class to map that xml also difficult .it has lots of child objects too . And there are some post , it is saying we should not use serialize. Thanks. – Sachin Silva Apr 10 '15 at 05:41
  • @paqogomez I didnt wrote the code yet. I am finding the best way to do it. If i use xml document ,XElement or xml writer then i need to write code to each and every tag. so it is difficult to write code for complex structure with 30 nodes xml . Please let me know is it the only way or there is a simple way to create class object of that xml. – Sachin Silva Apr 10 '15 at 05:47
  • I'm sorry, but this sounds like a lot of "do this for me, cause I dont want to". SO is not a site like that. You have to put forward the effort first, try something out, when it doesnt work post your errors and we help. – crthompson Apr 10 '15 at 15:08
  • @paqogomez It is not that i didnt tried and asking help. I have expiriance using other approches like xmldoc,xdocument , xml writer and XmlSerializer too. But the XML DOM object & xml writer is not easy to use with my xmls because i 've too many nodes . so it is difficult to write code for it. I have some good thoughts about XmlSerializer. I have created c# class for that xml request using xsd tool. But now i have a one problem can i use that created class to populate data and then serialize. – Sachin Silva Apr 10 '15 at 16:36

1 Answers1

2

Linq to Xml is a very succinct way to express Xml from linq queries.

Here is how you can build an Xml tree in LINQ to Xml (from Microsoft: https://msdn.microsoft.com/en-us/library/bb387089.aspx).

XElement contacts =
new XElement("Contacts",
    new XElement("Contact",
        new XElement("Name", "Patrick Hines"), 
        new XElement("Phone", "206-555-0144"),
        new XElement("Address",
            new XElement("Street1", "123 Main St"),
            new XElement("City", "Mercer Island"),
            new XElement("State", "WA"),
            new XElement("Postal", "68042")
        )
    )
);

Output:

<Contacts>
  <Contact>
    <Name>Patrick Hines</Name>
    <Phone>206-555-0144</Phone>
    <Address>
      <Street1>123 Main St</Street1>
      <City>Mercer Island</City>
      <State>WA</State>
      <Postal>68042</Postal>
    </Address>
  </Contact>
</Contacts>

Joseph Albahari's great c# bible C# 5.0 In a Nutshell has some great examples including a chapter on "Linq to XML". His free LinqPad application comes with some good examples like these ones below from chapter 10.

The examples below build XML directly from a Linq statements. You can see that it gives you more direct control over the output XML than direct serialization and simplifies creating more complex XML structures.

// Query Example 1

IQueryable<XElement> sqlQuery =
from c in Customers
    select 
        new XElement ("customer", new XAttribute ("id", c.ID),
            new XElement ("name", c.Name),
            new XElement ("buys", c.Purchases.Count)
        );
var customers = new XElement ("customers", sqlQuery);



// Query Example 2

new XElement ("customers",
from c in Customers
    let lastBigBuy = (
        from p in c.Purchases
        where p.Price > 1000
        orderby p.Date descending
        select p
    ).FirstOrDefault()
    select 
    new XElement ("customer", new XAttribute ("id", c.ID),
        new XElement ("name", c.Name),
        new XElement ("buys", c.Purchases.Count),
        new XElement ("lastBigBuy",
            new XElement ("description",
                lastBigBuy == null ? null : lastBigBuy.Description),
            new XElement ("price",
                lastBigBuy == null ? 0m : lastBigBuy.Price)
            )
        )
    )
WillC
  • 1,761
  • 17
  • 37
  • But my xml is a complex one and it has too many child object in side . So if i try this approach it will take lots of time and need careful attention on missing tags . Is this the best way of creating xml structure(or mapping to c# object model ) or is there any easy way other than manually creating. – Sachin Silva Apr 10 '15 at 05:59
  • Another possibility is using serialization of complex objects explained in DeepTownCitizens link above. – WillC Apr 10 '15 at 14:10
  • Added more advanced versions building directly from linq statements. – WillC Apr 10 '15 at 15:51