【问题标题】:How to get rid of square brackets[] after editing and saving an XML file编辑和保存 XML 文件后如何去掉方括号[]
【发布时间】:2014-12-17 09:55:13
【问题描述】:

如何去掉以下 XML 文档类型末尾的方括号

<!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"&gt;" 末尾没有 []

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

这是一个示例:

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

            }

        //}

    }

【问题讨论】:

  • 也许编译器厌倦了高嵌套级别,并且出于混淆而抛出了一些括号:-) 我会使用一些 XPath 表达式而不是 foreaching DOM...

标签: c#


【解决方案1】:

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

doc.DocumentType.InternalSubset = null;

【讨论】:

  • 在 4.0 中不能将 InternalSubset 设置为 null,它是只读属性
  • 在 4.0 (link) 中不是只读的。我尝试了上面的建议,但 InternalSubset 无论如何都是空的。也许与未考虑 null 的 XmlWriter 有关?
  • “在 4.0 中不能将 InternalSubset 设置为 null,它是只读属性”。 InternalSubset 对于 XmlDocument 是只读的。 InternalSubset 对于 XDocument 是可写的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多