编辑和保存XML文件后如何摆脱方括号[]

时间:2012-03-27 19:37:30

标签: c#

如何摆脱以下XML doctype

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

我有加载XML文件的代码,进行一些更改然后保存文件。

这是原始文件的标题(无方括号):

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

正如您所见,"SMIL20.dtd">"

末尾没有[]

当我转换文档时,它会创建一对方括号,如下所示“SMIL20.dtd”[]&gt;。

以下是一个示例:

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);

            }

        //}

    }

1 个答案:

答案 0 :(得分:5)

我自己也遇到过同样的问题。我想与那些寻求解决同一问题的人分享我的解决方案。这是:

doc.DocumentType.InternalSubset = null;