XML序列化的注释 [英] XML serialize annotations

查看:366
本文介绍了XML序列化的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我有我不想修改XML文件。
中的XElement类AddAnnotation功能提供了一个选项只添加内存的数据是没有序列号,而不是XML的一部分。

I have a situation where I have an xml file that I don't want to modify. The AddAnnotation function in XElement class provides an option to add memory-only data which is not serialized and not part of the XML.

我希望能够保存这些批注(例如:另一个XML文件),然后才能得到相同的对象反序列化两个XML和注解我有。

I want to be able to save these annotations (for example: to another xml file) and then to deserialize both the xml and the annotations in order to get the same object I had.

我不想改变原有的XML,这就是我使用批注的原因。

I don't want to change the original xml and that's the reason that I use annotations.

要总之,我希望能够自定义数据添加到一个XML文件。这个数据会不会当我序列化或将是XML的一部分,但我将能够轻松地检索原始XML的XML的一部分。

To summarize, I want to be able to add custom data to an xml file. This data won't be a part of the xml when I serialize it or it will be a part of the xml but I would be able to retrieve the original xml easily.

你有任何建议我该怎么做这样的事情?

Do you have any recommendation how I can do such a thing?

编辑:我应该使用XML处理指令?正在处理用于这种用途的指令?

Should I use xml processing instructions? Are processing instructions intended for this kind of usage?

推荐答案

这听起来好像是最简单的方法是使用常规的节点,但在不同的XML命名空间 - 即

It sounds to me like the simplest approach would be to use regular nodes, but in a different xml namespace - i.e.

<foo standardAttrubute="abc" myData:customAttribute="def">
    <standardElement>ghi</standardElement >
    <myData:customElement>jkl</myData:customElement>
</foo>



(其中 myData的的xmlns 别名命名空间URI)

(where myData is an xmlns alias for the namespace-uri)

在很多情况下,读者只能在命名空间(或默认/空白命名空间) - 在自定义命名空间值一般都跳过

In many cases, readers are only checking for data in their namespace (or the default/blank namespace) - values in custom namespaces are generally skipped.

要获得打包原始的XML,一个简单的方法是运行通过XSLT,只有尊重缺省的/原始的命名空间。

To get pack the original xml, one simple approach would be to run it through an xslt that only respects the default/original namespace.


XNamespace myData = XNamespace.Get("http://mycustomdata/");
XElement el = new XElement("foo",
    new XAttribute(XNamespace.Xmlns + "myData", myData.NamespaceName),
    new XAttribute("standardAttribute", "abc"),
    new XAttribute(myData + "customAttribute", "def"),
    new XElement("standardElement", "ghi"),
    new XElement(myData + "customAttribute", "jkl"));
string s = el.ToString();

要从一个的XElement 删除这些数据,或许是:

To remove such data from an XElement, perhaps:

    static void Strip(XElement el, XNamespace ns) {
        List<XElement> remove = new List<XElement>();
        foreach (XElement child in el.Elements()) {
            if (child.Name.Namespace == ns) {
                remove.Add(child);
            } else {
                Strip(child, ns);
            }
        }
        remove.ForEach(child => child.Remove());

        foreach (XAttribute child in
            (from a in el.Attributes()
             where a.Name.Namespace == ns
             select a).ToList()) {
            child.Remove();
        }
    }

这篇关于XML序列化的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆