Click here to Skip to main content
15,117,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have the following code:

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


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:

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


Please help.

Regards
Aman Chaurasia

What I have tried:

foreach (var a in section)
            {
                XmlDocument SectionTotal = new XmlDocument();
                SectionTotal.LoadXml(a.OuterXML);
Posted
Updated 27-May-18 23:32pm
Comments
GKP1992 28-May-18 4:25am
   
Did you try writing a.outerxml as it is in the VB code rather than a.OuterXML?
Primo Chalice 28-May-18 4:30am
   
Yes, the case is not the issue. "outerxml" is creating the issue.

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)[^]:
C#
SectionTotal.LoadXml(a.OuterXml);

[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]
   
v2
Comments
Primo Chalice 29-May-18 7:29am
   
I am still unable to resolve the issue. Please help.

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

This is the code. If I write, (SectionTotal.OuterXml), it complies and then shows an error on runtime.

When I write (a.OuterXml), it shows me the error mentioned above.

a is an object and section is XmlNodeList. How should I proceed?
Jochen Arndt 29-May-18 8:17am
   
SectionTotal.OuterXml compiles because SectionTotal is of type XmlDocument which has also the OuterXml property. The run time failure is sourced by having an empty XmlDocument in that case.

I don't know why it still fails when 'section' is of type XmlNodeList. You can try using a "manual" loop:
for (int i = 0; i < section.Count; i++)
{
XmlDocument SectionTotal = new XmlDocument();
SectionTotal.LoadXml(section[i].OuterXml);
}


Just for completeness:
Do you have a
using System.Xml;
statement on top of your file?
Primo Chalice 29-May-18 8:50am
   
Yes, I do have System.Xml.

Also, when I used your given code, the application went into break mode.
Jochen Arndt 29-May-18 9:07am
   
What do you mean by "break mode"?

Does it compile?
Then the property might be empty or NULL.
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".
   
Comments
Primo Chalice 28-May-18 4:31am
   
Actually, that is my question. It worked in Visual Basic as an object so what exactly can be the issue in C#?
lmoelleb 28-May-18 5:34am
   
Hard to say as you do not provide the types, and to be honest I don't know much about how VB reacts as I never used it. But there is a general design philosophy difference in C# and VB as far as I remember. C# is "never do anything that is not clearly the intend of the developer". VB is "if there is a reasonable option that is likely to be the intend of the developer, then do it". The VB approach is really nice.... until you have spend a lot of time tracking down when it got it wrong... then you lean to hate computers trying to be smart. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900