locked
Need for HttpRequest post,get,put,patch,merge and option for xml input string RRS feed

  • Question

  • User-148788041 posted

    Hi,

    i am able to consume web api from json string. how to write code when string input is xml string like the below HttpRequest post,get,put,patch,merge and option

    <book>
    <name>Sam</name>
    <price>Rs 123</price>
    </book>

    Monday, February 11, 2019 5:56 PM

All replies

  • User475983607 posted

    Please see your previous posts with the same subject.  These are just a few...

    https://forums.asp.net/p/2152149/6250157.aspx?Re+Web+Api+using+Xml+as+input

    https://forums.asp.net/t/2150988.aspx

    https://forums.asp.net/t/2148554.aspx

    https://forums.asp.net/t/2148398.aspx

    To answer this question, you generally need to manually convert the string into a type if you don't use standard XML serialization.

    namespace ConsoleCs
    {
    
        class Program
        {
    
            static void Main(string[] args)
            {
                string xmlDocumentText = "<book><name>Sam</name><price>Rs 123</price></book>";
                XmlSerializer serializer = new XmlSerializer(typeof(book));
                using (StringReader reader = new StringReader(xmlDocumentText))
                {
                    book b = (book)(serializer.Deserialize(reader));
                    Console.WriteLine(b.name);
                    Console.WriteLine(b.price);
                }
                
            }
        }
    
    
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class book
        {
    
            private string nameField;
    
            private string priceField;
    
            /// <remarks/>
            public string name
            {
                get
                {
                    return this.nameField;
                }
                set
                {
                    this.nameField = value;
                }
            }
    
            /// <remarks/>
            public string price
            {
                get
                {
                    return this.priceField;
                }
                set
                {
                    this.priceField = value;
                }
            }
        }
    
    
    }

    Please review all you r previous threads as there is a lot of really good advice.

    Monday, February 11, 2019 7:13 PM
  • User61956409 posted

    Hi Guhananth,

    i am able to consume web api from json string. how to write code when string input is xml string like the below HttpRequest post,get,put,patch,merge and option

    <book>
    <name>Sam</name>
    <price>Rs 123</price>
    </book>

    You can try to convert the xml string to json string first, and make request to your Web API with converted json data. Please refer to the following code snippet. 

    string xmlstring = "<book><name>Sam</name><price>Rs123</price></book>";
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlstring);
    
    string jsonText = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

    With Regards,

    Fei Han

    Thursday, February 14, 2019 6:40 AM