设置中的 XDocument
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法直接创建
XDocument
设置,因为XDocument
类不符合 criteria 设置用于确定是否可以使用某种类型:XDocument
提供了一种通过解析字符串来创建 XML 文档的方法,但它不是构造函数,而是静态Load
方法(该方法采用TextWriter
>,不是字符串)。所以它不适合在设置中使用。但是您可以对其进行子类化,并为子类提供类型转换器。幸运的是,使用类型转换器对
XDocument
进行子类化非常容易。首先,创建一个子类:该类使用此
TypeConverter
:设置好后,您可以编写如下代码:
字符串
将传递到类型转换器并解析(通过Load
)为XDocument
,然后将其顶级元素添加到MyXDocument
中。这与Settings.Designer.cs
中自动生成的代码使用的分配相同:现在您可以进入“设置”对话框并创建此类型的设置。您无法导航到“类型”对话框中的类型;您必须手动输入类型的全名(
XmlSettingsDemo.MyXDocument
是我的名称)。You can't create an
XDocument
setting directly, because theXDocument
class doesn't meet the criteria used by the Settings to determine if a type can be used:XDocument
provides a way to create an XML document by parsing a string, but it's not a constructor, it's the staticLoad
method (which takes aTextWriter
, 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:That class uses this
TypeConverter
:Once you have this set up, you can write code like this:
and the string
<foo/>
will get passed into the type converter and parsed (viaLoad
) into anXDocument
, whose top-level element then gets added to theMyXDocument
. This is the same assignment that the auto-generated code inSettings.Designer.cs
uses: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).