设置中的 XDocument

发布于 2024-11-18 05:58:24 字数 218 浏览 4 评论 0原文

我试图在 VS2010 的设置窗格中手动输入 XDocument 但没有成功。类型是 System.Xml.Linq.XDocument

我收到的消息是:

无法转换为类型的实例 'System.Xml.Linq.XDocument'

有谁知道解决这个问题的方法吗?

英石

I am trying to hand enter an XDocument in the settings pane of VS2010 without success. The type is System.Xml.Linq.XDocument

The message I get is:

Cannot be converted to an instance of type
'System.Xml.Linq.XDocument'

Does anyone know a way around this?

ST

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

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

发布评论

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

评论(1

夏日浅笑〃 2024-11-25 05:58:24

您无法直接创建 XDocument 设置,因为 XDocument 类不符合 criteria 设置用于确定是否可以使用某种类型:

应用程序设置可以存储为任何可 XML 序列化的数据类型或具有实现 ToString/FromString 的 TypeConverter。最常见的类型是字符串、整数和布尔值,但您也可以将值存储为颜色、对象或连接字符串。

XDocument 提供了一种通过解析字符串来创建 XML 文档的方法,但它不是构造函数,而是静态 Load 方法(该方法采用 TextWriter >,不是字符串)。所以它不适合在设置中使用。

但是您可以对其进行子类化,并为子类提供类型转换器。幸运的是,使用类型转换器对 XDocument 进行子类化非常容易。首先,创建一个子类:

[TypeConverter(typeof(MyXDocumentTypeConverter))]
public class MyXDocument : XDocument
{
}

该类使用此 TypeConverter

public class MyXDocumentTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return (sourceType == typeof (string));
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
        {
            MyXDocument d = new MyXDocument();
            d.Add(XDocument.Load(new StringReader((string) value)).Elements().First());
            return d;
        }
        return null;
    }
}

设置好后,您可以编写如下代码:

MyXDocument d = "<foo/>";

字符串 将传递到类型转换器并解析(通过 Load)为 XDocument,然后将其顶级元素添加到 MyXDocument 中。这与 Settings.Designer.cs 中自动生成的代码使用的分配相同:

return ((global::XmlSettingsDemo.MyXDocument)(this["Setting"]));

现在您可以进入“设置”对话框并创建此类型的设置。您无法导航到“类型”对话框中的类型;您必须手动输入类型的全名(XmlSettingsDemo.MyXDocument 是我的名称)。

You can't create an XDocument setting directly, because the XDocument class doesn't meet the criteria used by the Settings to determine if a type can be used:

Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/FromString. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string.

XDocument provides a way to create an XML document by parsing a string, but it's not a constructor, it's the static Load method (which takes a TextWriter, not a string). So it's not suited for use in the Settings.

But you can subclass it, and give the subclass a type converter. Fortunately, it's pretty easy to subclass XDocument with a type converter. First, create a subclass:

[TypeConverter(typeof(MyXDocumentTypeConverter))]
public class MyXDocument : XDocument
{
}

That class uses this TypeConverter:

public class MyXDocumentTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return (sourceType == typeof (string));
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
        {
            MyXDocument d = new MyXDocument();
            d.Add(XDocument.Load(new StringReader((string) value)).Elements().First());
            return d;
        }
        return null;
    }
}

Once you have this set up, you can write code like this:

MyXDocument d = "<foo/>";

and the string <foo/> will get passed into the type converter and parsed (via Load) into an XDocument, whose top-level element then gets added to the MyXDocument. This is the same assignment that the auto-generated code in Settings.Designer.cs uses:

return ((global::XmlSettingsDemo.MyXDocument)(this["Setting"]));

Now you can go into your Settings dialog and create a setting of this type. You can't navigate to the type in the Type dialog; you have to manually enter the full name of the type (XmlSettingsDemo.MyXDocument was the name of mine).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文