Reputation: 413
I have the xml request and I need to generate c# classes for list structure .
Request :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org"
xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays ">
<soapenv:Header/>
<soapenv:Body>
<tem:request>
<tem:id>1</tem:id>
<tem:list>
<arr:string>Item1</arr:string>
<arr:string>Item2</arr:string>
<arr:string>Item3</arr:string>
</tem:list>
</tem:request>
</soapenv:Body>
</soapenv:Envelope>
Can somebody help me with this ? Thanks
Upvotes: 0
Views: 801
Reputation: 2505
Since you don't have a WSDL file for the service, what you can do use the little known feature of Visual Studio which Paste XML as Class that uses class generation features introduced in .NET 4.5.
The steps to use this feature are:
Visual Studio will then populate the class file with the generated classes for your XML request.
Note: Your example XML is currently malformed the xmlns:tem
attribute is not closed on the envelope element. This feature will not work if the XML is malformed.
Upvotes: 1
Reputation: 34433
You do not need classes. I think in this case it is easier to parse a string and add just then items in the list. See code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
List<string> items = new List<string>(){ "Item1", "Item2", "Item3"};
string xml =
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org\"" +
" xmlns:arr=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"<tem:request>" +
"<tem:id>1</tem:id>" +
"<tem:list>" +
"</tem:list>" +
"</tem:request>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
XDocument doc = XDocument.Parse(xml);
XElement root = doc.Root;
XNamespace temNs = root.GetNamespaceOfPrefix("tem");
XNamespace arrNs = root.GetNamespaceOfPrefix("arr");
XElement list = doc.Descendants(temNs + "list").FirstOrDefault();
List<XElement> xItems = items.Select(x => new XElement(arrNs + "string", x)).ToList();
list.Add(xItems);
doc.Save(FILENAME);
}
}
}
Upvotes: 0