c# - 如何在编辑和保存 XML 文件后去掉方括号[]

标签 c#

如何去掉以下 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">"

末尾没有 []

当我转换文档时,它会创建一对方括号,如下所示“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);

            }

        //}

    }

最佳答案

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

doc.DocumentType.InternalSubset = null;

关于c# - 如何在编辑和保存 XML 文件后去掉方括号[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9896454/

相关文章:

c# - Select() 内部的 bool 值

c# - 如何在不破坏现有 Object.Equals() 的情况下仅检查两个对象的属性是否相等?

c# - Crystal 报表不显示任何数据

c# - 如何以编程方式更新 azure blob url

c# - 我应该在每个 TestMethod 中还是在 ClassInitialize 中设置模拟方法?

c# - 没有从 'type1' 到 'type2' 的隐式引用转换

c# - 在 C# 中交换 Powershell

c# - 如何刷新 DbContext

c# - 以编程方式更改 ListBox DataTemplate

c# - 冒号是什么意思?