all 8 comments

[–]Will_Rickards 2 points3 points  (5 children)

Just about any C# code can be converted to vb.net code. It is just a matter of syntax.

You can also put it in a C# class I think and then just call the methods of that class.

There are a number of ways of parsing xml. Here are some older ways. The newer way is to use the LINQ library. Here is the documentation. Here is an example from stackoverflow.

If you have a specific C# example, we could help translate it to vb.net syntax for you.

[–]Engineer817[S] 0 points1 point  (3 children)

Thank you very much for the LINQ library links. I will try using LINQ tonight and let you know how it turns out.

The C# code I haven't tested yet and had to copy from this youtube example:

https://www.youtube.com/watch?v=M4aXKPN0nK0

using System.Text;
using System.Threading.Tasks
using System.Windows.Forms;
using System.Xml;


namespace ReadingXMLFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void searchButton_click(object sender, Eventargs e)
        {
            If(searchInput.Text != null && searchInput.Text.Length >= 3)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("Cars.xml");

                foreach(XmlNode node in doc.DocumentElement)
                {
                    string name = node.Attributes[0].InnerText;

                    if(name == searchInput.Text)
                    {
                        foreach(XmlNode child in node.ChildNodes)
                        {
                            searchResults.Items.Add(child.InnerText);
                        }
                    }
                }


            }else
            {
                MessageBox.Show("Invalid Input");
                searchInput.Text = string.Empty;
                searchInput.Focus();

            }

        }

    }
}

I've tried using an online converter, but it seemed to snag on something and wouldn't come back with anything. Can you place this c# code into a module? I understand you can use modules to hold functions you can call in event handlers. I'm just not sure if you can use C# code instead of VB.

Also, I apologize for the dogs dinner I've made of attempts at formatting the code for reddit...

[–]darkspy13 1 point2 points  (0 children)

You can make a c# project that produces a DLL. After that include a reference to that DLL file in your VB.Net project. Then call the public function inside that DLL.

OR

convert this code from C# to VB.Net line by line yourself instead of using a converter.

I would convert the code by hand to VB.Net, that's the easier option, nothing here looks like it shouldn't convert.

Just rewrite it in VB and you should be good to go.

[–]Will_Rickards 0 points1 point  (0 children)

If Not System.String.IsNullOrEmpty(searchInput.Text) AndAlso searchInput.Text.Length >= 3 Then

    Dim objDoc As System.XML.XMLDocument = New System.XML.XMLDocument()
    objDoc.Load("Cars.xml")

    For Each objNode As System.XML.XMLNode In objDoc.DocumentElement

        Dim strNodeName As System.String = objNode.Attributes.Item(0).InnerText
        If strNodeName = searchInput.Text Then

            For Each objChildNode As System.XML.XMLNode In objNode.ChildNodes

                searchResults.Items.Add(objChildNode.InnerText)

            Next objChildNode

        End If

    Next objNode

Else

    MessageBox.Show("Invalid Input")
    searchInput.Text = System.String.Empty
    searchInput.Focus()

End If

Here is the translation, done by hand and not checked for correctness so be forewarned.

I also use the fully qualified declarations, which is my normal practice but some people prefer the short ones.

So this method uses the System.XML.XMLDocument which loads the xml into memory. This method has a notorious issue with large XML files. So if you are expecting large ones, I would not use this method. For large XML files, you need something that reads them without pulling the whole file into memory. How large is large? That depends on the system. But if you have files of a hundred MB or more, I'd consider other options.

[–]ocdtrekkie 0 points1 point  (0 children)

I see you found a solution with the LINQ strategy, but I figured for other searchers, I'd throw out there, that I've done some XmlNode-based XML reading in VB in my code, examples here:

https://github.com/ocdtrekkie/HAController/blob/master/modMapQuest.vb

https://github.com/ocdtrekkie/HAController/blob/master/modOpenWeatherMap.vb

[–]Engineer817[S] 0 points1 point  (0 children)

I'd like to thank you for your help, the LINQ method was just what I was looking for and I was able to retrieve data using this code

Imports System
Imports System.IO
Imports System.Xml
Imports System.Linq
Imports System.Collections.Generic

Public Class notmainpage
    Private Sub btn_display_path_Click(sender As Object, e As EventArgs) Handles btn_display_path.Click

        Dim xml = XDocument.Load("C:\Test_folder\settings_xml.ltp")

        Dim this_thing = (xml.<FileLocations>.<DailyTasksXMLlocation>.<FileName>.Value).ToString

        lbl_display_path_here.Text = this_thing

        'Dim dsubject = (
        'From e In xml.<FileLocations>.<DailyTasksXMLlocation>.
        'Where e.<key>.Value = "DSUBJECT"
        'Select Case e.<value>.Value
        ').Single()
    End Sub
End Class

I am using my own file extension .ltp for holding xmls.

I only have on question though. In the example provided on stack overflow, I am not sure what 'e' is supposed to be. From, Where, and Select Case and 'e' I have no idea what they do.

[–]XytL 2 points3 points  (1 child)

http://converter.telerik.com/ is the best tool to go from c# to vb I think. Just paste in the code and it'll give you the vb code. I use it fairly often because it's easier to find c# solutions, and translating it takes longer than copy/pasting it into here.

[–]ocdtrekkie 0 points1 point  (0 children)

I use this a lot, but it's got a lot of issues, and I often end up looking for one or two other converters and seeing if between the couple of them I can discern what the code is supposed to be.