user1577242
user1577242

Reputation: 413

Generating C# classes based on XML request using ArrayOfString

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

Answers (2)

Adrian Sanguineti
Adrian Sanguineti

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:

  1. Create Class file that the XML to be inserted into.
  2. With the cursor inside the class file, click Edit -> Paste Special -> Paste XML as Classes.

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

jdweng
jdweng

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

Related Questions