Question

I have some old J# code that I am moving to C#

  XmlNodeList itemTransferOutNodes = 
                 strXML.GetElementsByTagName("ItemTransferOut");
  XmlElement itemInfo = 
                 itemTransferOutNodes.Item(itemTrOutNodesCnt)
                                     .ChildNodes.Item(0)
                                     .get_Item("itemInfo");

I dont see in C# API of XmlNodeList method called get_Item. To what I should change get_Item in c#.

Thanks .

Was it helpful?

Solution

J# does not have support for properties like C# does, so they are "faked" by using methods instead. You can find more information on that matter on MSDN. If a C# object has a property named SomeProperty:

 public class Dummy {
      public string SomeProperty { get; set; }
 }

in J#, you'll have to call get_SomeProperty() and set_SomeProperty(string value):

 public class Dummy
 {
      private String someProperty;

      /** @property */
      public void set_SomeProperty(String val) { 
          someProperty = val; 
      }

      /** @property */
      public String get_SomeProperty() { 
          return someProperty; 
      }
 }

And the other way around is true.

If you find in J# a class method called get_xxx or set_xxx, it's most likely that in C#, the object has a property named xxx.

So basically, as others mentionned, you have to use the Item property in your code :

XmlNodeList itemTransferOutNodes = 
                  strXML.GetElementsByTagName("ItemTransferOut");

XmlElement itemInfo = 
                  itemTransferOutNodes.Item(itemTrOutNodesCnt)
                  .ChildNodes.Item(0).Item["itemInfo"];

Hope that helps :)

OTHER TIPS

There are several ways, I would recommend LINQ to XML

Without seeing your XML I guess it would be something like:

strXml.Root
      .Decendants("ItemTransferOut")
      .First(xele => xele.Name.LocalName == "itemInfo")
XmlElement itemInfo = itemTransferOutNodes.Item(itemTrOutNodesCnt)
                                          .ChildNodes
                                          .Item(0)["itemInfo"]; 

XMLNode.Item documentation on MSDN

Use XmlNode.Item Property (String) . from msdn:

Gets the first child element with the specified Name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top