locked
Returning XML as a string RRS feed

  • Question

  • User-1458727574 posted

    Hi,

    I have a small internal class:

            internal class Utf8StringWriter : System.IO.StringWriter
            {
                public override Encoding Encoding => Encoding.UTF8;
            }
    

    I have a short function that returns the XML as a string:

            private string XMLToString(AR0032_LIST ARList)
            {
                Utf8StringWriter stringWriter = new Utf8StringWriter();
                XmlSerializer serialiser = new XmlSerializer(ARList.GetType());
    
                serialiser.Serialize(stringWriter, ARList);
    
                return stringWriter.ToString();
            }
    

    If any of the values in the XML are null, they are not included in the output. Is there a way to get them included?

    Thanks.

    Monday, October 1, 2018 11:25 AM

Answers

  • User283571144 posted

    Hi AnyUserNameThatLetsMeIn,

    According to your description, I suggest you could consider using  [XmlElement(IsNullable = true)] attribute, then it will generate the xml nodes in the xml string as below format without excluding it.

          <name xsi:nil="true" />

    More details, you could refer to below sample:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace EmptyWebFormTest
    {
        public partial class XMLTestWebForm : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                var re = ConvertToString();
                int i = 0;
            }
    
    
            public string ConvertToString()
            {
                books bks = new books();
    
                bks.Books = new books.book[] { new books.book {   records = new books.book.record[] { new books.book.record { borrowDate = DateTime.Now.ToString(), returnDate = DateTime.Now.ToString() } } } };
    
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(books));
    
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
    
                using (StringWriter textWriter = new StringWriter())
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                    {
                        serializer.Serialize(xmlWriter, bks);
                    }
                    return textWriter.ToString(); //This is the output as a string
                }
            }
    
            [System.Xml.Serialization.XmlRoot("books")]
            public class books
            {
                public int bookNum { get; set; }
                public book[] Books { get; set; }
    
                public class book
                {
                    [XmlElement(IsNullable = true)]
                    public string name { get; set; }
                    public class record
                    {
                        public string borrowDate { get; set; }
                        public string returnDate { get; set; }
                    }
                    public record[] records { get; set; }
                }
            }
        }
    }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 2, 2018 2:32 AM

All replies

  • User283571144 posted

    Hi AnyUserNameThatLetsMeIn,

    According to your description, I suggest you could consider using  [XmlElement(IsNullable = true)] attribute, then it will generate the xml nodes in the xml string as below format without excluding it.

          <name xsi:nil="true" />

    More details, you could refer to below sample:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace EmptyWebFormTest
    {
        public partial class XMLTestWebForm : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                var re = ConvertToString();
                int i = 0;
            }
    
    
            public string ConvertToString()
            {
                books bks = new books();
    
                bks.Books = new books.book[] { new books.book {   records = new books.book.record[] { new books.book.record { borrowDate = DateTime.Now.ToString(), returnDate = DateTime.Now.ToString() } } } };
    
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(books));
    
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
    
                using (StringWriter textWriter = new StringWriter())
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                    {
                        serializer.Serialize(xmlWriter, bks);
                    }
                    return textWriter.ToString(); //This is the output as a string
                }
            }
    
            [System.Xml.Serialization.XmlRoot("books")]
            public class books
            {
                public int bookNum { get; set; }
                public book[] Books { get; set; }
    
                public class book
                {
                    [XmlElement(IsNullable = true)]
                    public string name { get; set; }
                    public class record
                    {
                        public string borrowDate { get; set; }
                        public string returnDate { get; set; }
                    }
                    public record[] records { get; set; }
                }
            }
        }
    }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 2, 2018 2:32 AM