Home > Software engineering >  How to add the same XML tags multiple times, with different content value in (c#) .Net Core
How to add the same XML tags multiple times, with different content value in (c#) .Net Core

Time:10-01

I am creating and API in .Net core using webservice .wsdl file, I have hardcoded the xml like below: enter image description here

 XDocument xDocument = XDocument.Parse(
                          "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Header/>\r\n<soapenv:Body>\r\n<MyServiceRequest>\r\n<ITEMSLIST>\r\n<ITEMS>\r\n<ID>$"  request.ID "</ID>\r\n<NAME>"   request.NAME  "</NAME>\r\n</ITEMS>\r\n</ITEMSLIST>\r\n</acc:MyServiceRequest>\r\n</soapenv:Body>\r\n</soapenv:Envelope>");

 var xmlRequestBody = xDocument.ToString();

Values are adding in array from this model:

Items[] request

public class Items
    {
        public string ID { get; set; }
        public string NAME { get; set; }
    }

I am adding values dynamically, for single value it is working fine, but when adding multiple values it is not working. XML file is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header />
      <soapenv:Body>
        <acc:MyServiceRequest>
          <ITEMSLIST>
            <ITEMS>
              <ID>06285883</ID>
              <NAME>John</NAME>
            </ITEMS>
          </ITEMSLIST>
        </acc:MyServiceRequest>
      </soapenv:Body>
    </soapenv:Envelope>

for multiple values the xml should look like this, before sending the request. the values should be filled dynamically from the request model.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header />
      <soapenv:Body>
        <acc:MyServiceRequest>
          <ITEMSLIST>
            <ITEMS>
              <ID>06285883</ID>
              <NAME>John</NAME>
            </ITEMS>
             <ITEMS>
              <ID>06285231</ID>
              <NAME>Sara</NAME>
            </ITEMS>
          </ITEMSLIST>
        </acc:MyServiceRequest>
      </soapenv:Body>
    </soapenv:Envelope>

Can anyone guide How I can add multiple values in the same xml?

CodePudding user response:

* I'm trying to give the main idea

  1. I just removed all those \r\n.
  2. Found the ITEMSLIST element out side of the loop.
  3. Iterat all items and for each one, make an XElement object and add it to itemsList element.
  4. In the end, the result is in xDocument object.
private static XElement xDocument = XElement.Parse(
@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
   <soapenv:Header/>
   <soapenv:Body>
     <MyServiceRequest>
        <ITEMSLIST>
        </ITEMSLIST>
     </MyServiceRequest>
   </soapenv:Body>
</soapenv:Envelope>");


public void Test()
{
    Items[] items = new Items[]
    {
        new Items { ID = "06285883", NAME = "John" },
        new Items { ID = "06285231", NAME = "Sara" }
    };

    var itemsList = xDocument.Descendants("ITEMSLIST").Single();
    foreach (var item in items)
    {
        XElement element = new XElement("ITEMS");
        element.Add(new XElement("ID", item.ID));
        element.Add(new XElement("NAME", item.NAME));

        itemsList.Add(element);
    }

    Console.WriteLine(xDocument);
}
  • Related