我可以使用 Linq-to-xml 来保存我的对象状态,而不必使用/知道 Xpath 和 Xpath 吗? XSD 语法?

发布于 2024-08-30 18:05:36 字数 210 浏览 4 评论 0原文

我可以使用 Linq-to-xml 来保存我的对象状态,而不必使用/知道 Xpath 和 Xpath 吗? XSD 语法?

IE。真正寻找简单但灵活的方法来持久化对象数据图(例如,有 2 或 3 个具有关联的类) - 如果 Linq-to-xml 就像说“将此图持久化为 XML”一样简单,那么您也可以通过 Linq 查询它,或者再次将其加载到内存中/更改/然后重新保存到 xml 文件。

Can I use Linq-to-xml to persist my object state without having to use/know Xpath & XSD Syntax?

ie. really looking for simple but flexible way to persist a graph of object data (e.g. have say 2 or 3 classes with associations) - if Linq-to-xml were as simple as saying "persist this graph to XML", and then you could also query it via Linq, or load it into memory again/change/then re-save to the xml file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何止钟意 2024-09-06 18:05:36

您通常不需要 XPath 或 XSD 来使用 LINQ-to-XML,但它也不会执行您想要的操作。 XmlSerializer 很接近,但它是一个序列化器,而不是序列化器。

DataContractSerializer (.NET 3.0) 确实通过重载构造函数之一提供图形支持,但不提供对 xml 的完全控制。

BinaryFormatter 提供图形支持和基于元数据/类型的工作,但如果您更改程序集,它会非常脆弱,并且不可在平台之间移植。

我怀疑要弄清楚的是:我的数据是树还是图? XmlSerializer 可能已经满足您的需要。


using System;
using System.Runtime.Serialization;
using System.IO;
[DataContract]
public class Outer {
    [DataMember]
    public Inner Inner { get; set; }
}
[DataContract]
public class Inner {
    [DataMember]
    public Outer Outer { get; set; }
}
class Program {
    static void Main() {
        // make a cyclic graph
        Outer outer = new Outer(), clone;
        outer.Inner = new Inner { Outer = outer };

        var dcs = new DataContractSerializer(typeof(Outer), null,
            int.MaxValue, false, true, null);
        using (MemoryStream ms = new MemoryStream()) {
            dcs.WriteObject(ms, outer);
            ms.Position = 0;
            clone = (Outer)dcs.ReadObject(ms);
        }
        Console.WriteLine(ReferenceEquals(
            clone, clone.Inner.Outer)); // true
    }
}

You don't usually need XPath or XSD to use LINQ-to-XML, but it also won't do what you want. XmlSerializer comes close , but is a tree serializer, not a graph serializer.

DataContractSerializer (.NET 3.0) does offer graph support via one of the overloaded constructors, but doesn't offer full control over the xml.

BinaryFormatter offers graph support and metadata-/type-based workings, but is very brittle if you ever change your assembly, and is not portable between platforms.

I suspect the thing to figure out is: is my data a tree or a graph? XmlSerializer may already do what you need.


using System;
using System.Runtime.Serialization;
using System.IO;
[DataContract]
public class Outer {
    [DataMember]
    public Inner Inner { get; set; }
}
[DataContract]
public class Inner {
    [DataMember]
    public Outer Outer { get; set; }
}
class Program {
    static void Main() {
        // make a cyclic graph
        Outer outer = new Outer(), clone;
        outer.Inner = new Inner { Outer = outer };

        var dcs = new DataContractSerializer(typeof(Outer), null,
            int.MaxValue, false, true, null);
        using (MemoryStream ms = new MemoryStream()) {
            dcs.WriteObject(ms, outer);
            ms.Position = 0;
            clone = (Outer)dcs.ReadObject(ms);
        }
        Console.WriteLine(ReferenceEquals(
            clone, clone.Inner.Outer)); // true
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文