Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 有没有办法创建不可变(只读)XDocument?_.net_Api_Linq To Xml_Xelement - Fatal编程技术网

.net 有没有办法创建不可变(只读)XDocument?

.net 有没有办法创建不可变(只读)XDocument?,.net,api,linq-to-xml,xelement,.net,Api,Linq To Xml,Xelement,我有一个返回的API,我希望那些XElement后面的文档是不可变的(只读)。我需要它是为了: 不允许开发人员意外更改它:) 提高性能-在某些情况下,创建XDocument的副本可能是一项性能“繁重”的操作 似乎不可能继承和重写XDocument/XElement/XContainer中的必要行为,因为那里的所有虚拟方法都标记为internal: internal virtual void XContainer.AddAttribute(XAttribute a) { } 所以我的问题是-

我有一个返回的API,我希望那些
XElement
后面的文档是不可变的(只读)。我需要它是为了:

  • 不允许开发人员意外更改它:)
  • 提高性能-在某些情况下,创建
    XDocument
    的副本可能是一项性能“繁重”的操作
似乎不可能继承和重写
XDocument
/
XElement
/
XContainer
中的必要行为,因为那里的所有虚拟方法都标记为
internal

internal virtual void XContainer.AddAttribute(XAttribute a)
{
}

所以我的问题是-有没有办法实现它,或者最好使用一个不同的API来返回类似于
XPathNavigator
的内容,或者最好使用自己的类,如
IReadOnlyXElement
,等等?

您可以创建一个类似于
ReadOnlyCollection
XElement
包装器

公共密封类ReadOnlyXElement
{
私有只读元素;
公共字符串值
{
获取{return}element.Value;}
}
公共ReadOnlyXElement(XElement元素)
{
_元素=元素;
}
公共IEnumerable元素()
{
foreach(在_element.Elements()中的变量child)
{
返回新的ReadOnlyXElement(子元素);
}
}
公共IEnumerable元素(XName XName)
{
foreach(变量子元素在_element.Elements(xname))中)
{
返回新的ReadOnlyXElement(子元素);
}
}
}

恕我直言,为与XDocuments/XElements交互创建自己的包装器类可能更好。然后,您可以限制开发人员在代码中写入文件的能力


我之所以说限制,是因为有了足够的信息(位置、模式(如果需要)),开发人员就可以使用stock XmlClass做任何他们想做的事情。最终的结果是使文件在磁盘上只读,并确保他们(开发人员、用户)没有权限更改文件的只读访问权限。

我怀疑管理员是否仍在等待答案,但也许其他人会发现它有用

您可以通过使用XDocument的更改事件使其不可变:

    class Program
    {
        static void Main(string[] args)
        {
            var xdoc = XDocument.Parse("<foo id=\"bar\"></foo>");
            xdoc.Changing += (s, ev) =>
            {
                throw new NotSupportedException("This XDocument is read-only");
            };

            try
            {
                xdoc.Root.Attribute("id").Value = "boo";
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: " + e.Message);
            }

            Console.WriteLine("ID on exit: " + xdoc.Root.Attribute("id").Value);

            Console.ReadKey();
        }
    }

// Console output:
// EXCEPTION: This XDocument is read-only
// ID on exit: bar
类程序
{
静态void Main(字符串[]参数)
{
var xdoc=XDocument.Parse(“”);
xdoc.变化+=(s,ev)=>
{
抛出新的NotSupportedException(“此XDocument是只读的”);
};
尝试
{
xdoc.Root.Attribute(“id”).Value=“boo”;
}
捕获(例外e)
{
Console.WriteLine(“异常:+e.Message”);
}
WriteLine(“退出时的ID:+xdoc.Root.Attribute(“ID”).Value”);
Console.ReadKey();
}
}
//控制台输出:
//例外:此XDocument是只读的
//出口ID:bar
这不是最好的解决方案,但它确实提供了防止意外更改的基本机制

    class Program
    {
        static void Main(string[] args)
        {
            var xdoc = XDocument.Parse("<foo id=\"bar\"></foo>");
            xdoc.Changing += (s, ev) =>
            {
                throw new NotSupportedException("This XDocument is read-only");
            };

            try
            {
                xdoc.Root.Attribute("id").Value = "boo";
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: " + e.Message);
            }

            Console.WriteLine("ID on exit: " + xdoc.Root.Attribute("id").Value);

            Console.ReadKey();
        }
    }

// Console output:
// EXCEPTION: This XDocument is read-only
// ID on exit: bar