-1

Hi i need help in converting the XML to Object class so whenever i convert that object back into the XML format i got the same output as expected in some API request. For now i used online tool(https://json2csharp.com/code-converters/xml-to-csharp) that converts that model into XML but still it's not as expected. like after convert the root attribute got missed i.e. xmlns:p also the <p: starting tag at name, so calling the API fails because of this as they expected me to send along

Example XML is here:

<p:EIDVBusinessSearch xmlns:p="example.com" xmlns:xsi="abc.com" xsi:schemaLocation="cde.com">
  <PermissiblePurpose>
    <GLB>{{GLB}}</GLB>
    <DPPA>{{DPPA}}</DPPA>
    <VOTER>{{VOTER}}</VOTER>
 <PermissiblePurpose>
</p:EIDVBusinessSearch>.
Khan
  • 41
  • 6
  • see if this answers your question. https://stackoverflow.com/questions/3187444/convert-xml-string-to-object – mabiyan Dec 15 '22 at 07:34
  • Your XML doesn't have closing tag for ``. So your XML is invalid. Replace `` with `` before `` line. – Yong Shun Dec 15 '22 at 07:48
  • @YongShun yes you're right i just mistyped it. But do you any idea of my actual issue. – Khan Dec 15 '22 at 08:36

1 Answers1

0

I have created a desktop WinForm application using .NET 6 framework and write the following code. I hope this code will help you in some way to fix your problem.

Note: you have to use XmlAttribute Namespace properly to fix your missing attribute issue.

However, the starting p: will stay remain missing.

EidBusinessSearch class:

[XmlRoot(ElementName = "EIDVBusinessSearch", Namespace = "example.com")]
public class EIDVBusinessSearch
{

    [XmlElement(ElementName = "PermissiblePurpose", Namespace = "")]
    public PermissiblePurpose? PermissiblePurpose { get; set; }

    [XmlAttribute(AttributeName = "p", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string? P { get; set; }

    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string? Xsi { get; set; }

    [XmlAttribute(AttributeName = "schemaLocation", Namespace = "abc.com")]
    public string? SchemaLocation { get; set; }

    [XmlText]
    public string? Text { get; set; }
}

PermissiblePurpose

[XmlRoot(ElementName = "PermissiblePurpose", Namespace = "")]
public class PermissiblePurpose
{

    [XmlElement(ElementName = "GLB", Namespace = "")]
    public string? GLB { get; set; }

    [XmlElement(ElementName = "DPPA", Namespace = "")]
    public string? DPPA { get; set; }

    [XmlElement(ElementName = "VOTER", Namespace = "")]
    public string? VOTER { get; set; }
}

Method to Convert XML to Object:

private void ConvertXMLtoObject()
    {
        try
        {
            string xml = "<p:EIDVBusinessSearch xmlns:p=\"example.com\" xmlns:xsi=\"abc.com\" xsi:schemaLocation=\"cde.com\">" +
                "\r\n  <PermissiblePurpose>" +
                "\r\n    <GLB>{{GLB}}</GLB>" +
                "\r\n    <DPPA>{{DPPA}}</DPPA>" +
                "\r\n    <VOTER>{{VOTER}}</VOTER>" +
                "\r\n </PermissiblePurpose>" +
                "\r\n</p:EIDVBusinessSearch>";

            XmlSerializer serializer = new(typeof(EIDVBusinessSearch));

            EIDVBusinessSearch obj = new();

            using StringReader reader = new(xml);
            obj = (EIDVBusinessSearch)serializer.Deserialize(reader);


        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Application.Exit();
        }
    }

Convert Object to XML:

private void ConvertObjectToXML()
    {
        try
        {
            EIDVBusinessSearch obj = new()
            {
                P = "example.com",
                PermissiblePurpose = new()
            };
            obj.PermissiblePurpose.GLB = "GLB";
            obj.PermissiblePurpose.DPPA = "DPPA";
            obj.PermissiblePurpose.VOTER = "VOTER";

            XmlSerializer serializer = new(obj.GetType());

            using StringWriter writer = new();
            serializer.Serialize(writer, obj);
            string xml = writer.ToString();

            label1.Text = xml;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Application.Exit();
        }
    }

Output:

Output: XML String

  • Thank you very much for the effort mate. One tiny thing is still missing that is custom attribute of root element i.e. xmlns:p. Also if you noticed that is also present in root element tag – Khan Dec 15 '22 at 09:41
  • Yes, I edited my answer because if you look at `EIDVBusinessSearch`, you can see that P is also an attribute. but in your case, you need the same root element prefix. – Tiny Developer Dec 15 '22 at 09:44
  • 1
    @Khan add this line in the obj initialization to get `xmlnsLp ` `EIDVBusinessSearch obj = new() { P = "example.com", PermissiblePurpose = new() };` – Tiny Developer Dec 15 '22 at 09:48
  • Already tried that too. It worked but still my issue is slightly resolved like if you just see the output you can see xmlns:p attribute but the tag name still persists with – Khan Dec 15 '22 at 12:35