locked
How to add doctype after declaration in an existing xml string RRS feed

  • Question

  • User749309136 posted

    I created a C# class from a DTD document using the xsd.exe

    Created an object of the generated class ' myService '

    I am using bellow function to get xml from an C# object:

     public string ToXML(myService obj)
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                
                var stringwriter = new System.IO.StringWriter();           
                var serializer = new XmlSerializer(typeof(myService));
                serializer.Serialize(stringwriter, obj, ns);
                var str = stringwriter.ToString();            
                return str;


            }

    The output string is something like bellow:

    <?xml version="1.0" encoding="utf-8"?>
    <myService version="1.4" Code="code1" xmlns="http://tempuri.org/myService_v1">
      <inquiry>
        <oInquiry oCode="FHJJJJ7788888" />
      </inquiry>
    </myService >

    Now I want to add DocType (<!DOCTYPE .... >) element to this string.

    Even after going through lot of googling and posts, I found one solution to add doctype, but it removes xml declaration at top ???

    I want my xml string contain doctype too as bellow:

    <?xml version="1.0" encoding="utf-8"?>

    <!DOCTYPE myService PUBLIC "-//dddd//ddddd v1//EN" "http://dtd.dddd.com/myService_v1.dtd">


    <myService version="1.4" Code="code1" xmlns="http://tempuri.org/myService_v1">
      <inquiry>
        <oInquiry oCode="FHJJJJ7788888" />
      </inquiry>
    </myService >

    Please help,

    Thursday, May 30, 2013 4:59 AM

Answers

  • User749309136 posted

    Got the ans by self (aftters investing hrs on web search). Will help others, as nothing exactly, I can find on web:

        public string foo(myService obj)
            {          
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");

                var stringwriter = new System.IO.StringWriter();
                using (XmlWriter writer = XmlWriter.Create(stringwriter))
                {
                    writer.WriteDocType("myService ", "-//cccc//DTD ffffff myService v1//EN", "http://dtd.dddd.com/myService _v1.dtd", null);
                    var serializer = new XmlSerializer(typeof(myService ));
                    serializer.Serialize(writer, obj, ns);
                }
                var str = stringwriter.ToString();

                var str1 = str.Replace("<q1:", "<").Replace("</q1:", "</");

                return str;
            }

    cheers :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2013 7:23 AM

All replies

  • User749309136 posted

    Got the ans by self (aftters investing hrs on web search). Will help others, as nothing exactly, I can find on web:

        public string foo(myService obj)
            {          
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");

                var stringwriter = new System.IO.StringWriter();
                using (XmlWriter writer = XmlWriter.Create(stringwriter))
                {
                    writer.WriteDocType("myService ", "-//cccc//DTD ffffff myService v1//EN", "http://dtd.dddd.com/myService _v1.dtd", null);
                    var serializer = new XmlSerializer(typeof(myService ));
                    serializer.Serialize(writer, obj, ns);
                }
                var str = stringwriter.ToString();

                var str1 = str.Replace("<q1:", "<").Replace("</q1:", "</");

                return str;
            }

    cheers :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2013 7:23 AM
  • User260886948 posted

    Hi,

    I am very glad that you have solved your problem by yourself.

    If you have any other problem, welcome to post it in the asp.net forums.

    Best Regards,
    Amy Peng 

    Friday, May 31, 2013 5:22 AM