如何更正C#中的以下错误? [英] How do I correct the following error in C#?

查看:105
本文介绍了如何更正C#中的以下错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有以下代码:



Hello,

I have the following code:

foreach (var a in section)
            {
                XmlDocument SectionTotal = new XmlDocument();
                SectionTotal.LoadXml(a.OuterXML);





在提到的代码中,我在OuterXML中收到错误,说明:


'object'不包含'OuterXML'的定义,也没有扩展方法'OuterXML'接受'object'类型的第一个参数'可以找到(你错过了使用指令或程序集引用吗?)



Visual Basic中的C#代码工作正常:





In the mentioned code, I am getting an error in OuterXML, stating:

'object' does not contain a definition for 'OuterXML' and no extension method 'OuterXML' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

The C# code in Visual Basic is working fine:

For Each a In section
            Dim SectionTotal As New XmlDocument()
            SectionTotal.LoadXml(a.outerxml)





请帮忙。



问候

Aman Chaurasia



我尝试过: < br $>




Please help.

Regards
Aman Chaurasia

What I have tried:

foreach (var a in section)
            {
                XmlDocument SectionTotal = new XmlDocument();
                SectionTotal.LoadXml(a.OuterXML);

推荐答案

错误信息非常清楚。对象 a 没有 OuterXML 的定义。



很高兴分别知道 a 的类型部分。然后可以检查该类型的文档以获取可用的方法和属性及其语法。



如果是XmlNode.OuterXml属性(System.Xml) [ ^ ]:

The error message is quite clear. The object a does not has a definition for OuterXML.

Would be good to know the type of a respectively section. Then the documentation for that type can be checked for the available methods and properties and their syntax.

If it is the XmlNode.OuterXml Property (System.Xml)[^]:
SectionTotal.LoadXml(a.OuterXml);





C#区分大小写,而VB .Net则不区分大小写。



更准确地说:VB编译器将检查名称是否与不区分大小写匹配,然后选择正确的区分大小写的名称。



所以它不关心你是否使用 OuterXML outerxml 使用VB,因为编译器会将其解析为 OuterXml 并在内部使用它。但这不适用于必须使用确切名称的C#。

[/ EDIT]



C# is case sensitive while VB .Net is not.

More precisely: The VB compiler will check if a name matches case insensitive and select then the correct case sensitive name.

So it does not care if you use OuterXML or outerxml with VB because the compiler will resolve that to OuterXml and use it internally. But that does not apply to C# where you have to use the exact name.
[/EDIT]


将var更改为您期望的实际类型部分集合。显然,集合部分的类型不允许编译器推断除对象之外要使用的特定类型。
Change "var" to the actual type you expect in the section collection. Apparently the type of the collection section does not allow the compiler to infer a specific type to use besides "object".


这篇关于如何更正C#中的以下错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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