3

How do I get rid of the square brackets at the end of the following XML doctype

<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd"[]>

I have code that loads an XML file , make some changes then save the file.

Here is the heading of the original file(No square brackets):

<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">

As you can see there is no [] at the end of "SMIL20.dtd">"

when I convert the document it creates a pair of square brackets as follows "SMIL20.dtd"[]>.

Here is a sample:

class FileWorker
{
    public static void UpdateFile(string myfile)
    {
            var doc = XDocument.Load(myfile);           
            try
            {
                foreach (var elem in doc.Elements())
                {

                    if (elem.Name.LocalName == "type")
                    {
                        foreach (var child in elem.Elements())
                        {
                            if (child.Name.LocalName == "head")
                            {
                                foreach (var gc in child.Elements())
                                {
                                    if (Configuration.Cdntosync == 0)
                                    {
                                        if (gc.Attributes().Any(att => att.Name.LocalName == "name" && att.Value == "httpBase"))
                                        {
                                            gc.RemoveAttributes();
                                            gc.Add(new XAttribute("base", "rtmp://mybase"));
                                        }
                                    }
                                    else if (Configuration.Cdntosync == 1)
                                    {
                                        if(gc.Attributes().Any(att => att.Name.LocalName == "name" && att.Value == "vod"))
                                        {
                                            gc.Remove();
                                        }
                                        gc.RemoveAttributes();
                                        if (basecheck == false)
                                        {
                                            gc.Add(new XAttribute("base", "rtmp://mybase2"));
                                            basecheck = true;
                                        }
                                    }
                                }

                                basecheck = false;
                            }
                            else if (child.Name.LocalName == "body")
                            {
                                foreach (var gc in child.Elements())
                                {
                                    if (gc.Name.LocalName == "switch")
                                    {
                                        foreach (var video in gc.Elements())
                                        {
                                            foreach (var att in video.Attributes())
                                            {
                                                if (att.Name.LocalName == "src")
                                                {

                                                    att.Value = att.Value.Replace("v1/test/", "mp4:Flvstream/");

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }


                    doc.Save(myfile);

                }

            }
        catch(Exception ex)
            {
                logger.Log(ExceptionLogger.LogLevel.ERROR, ex);

            }

        //}

    }
DJ Quimby
  • 3,669
  • 25
  • 35
user1204195
  • 493
  • 5
  • 12
  • 19
  • Maybe the compiler got tired of the high nesting level, and threw some brackets out of confusion :-) I'd use some XPath expressions instead of foreaching the DOM... – seldary Aug 03 '12 at 21:41

1 Answers1

6

Just come across the same problem myself. I would like to share my solution to those searching for resolving the same problem. Here it is:

doc.DocumentType.InternalSubset = null;
b4hand
  • 9,550
  • 4
  • 44
  • 49
ken
  • 61
  • 1
  • 2
  • you cant set InternalSubset to null in 4.0, it is a readonly property – Les Aug 03 '12 at 23:49
  • It's not readonly in 4.0 ([link](http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocumenttype.internalsubset%28v=vs.110%29.aspx)). I tried the suggestion above but the InternalSubset was null anyway. Maybe something to do with the XmlWriter not taking null into account? – Alex Kamburov Mar 25 '14 at 12:34
  • 1
    _"you cant set InternalSubset to null in 4.0, it is a readonly property"_ . InternalSubset is readonly for XmlDocument. InternalSubset is writeable for XDocument. – wisbucky Dec 16 '14 at 23:42