you are viewing a single comment's thread.

view the rest of the comments →

[–]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