ok, I am working on a program for work and I am stuck on trying to make a new XML file from VB.NET 2010. I have the old code which was done in C# and it works but even using a C# to VB converter didn't help. I will paste in both the C# and what I have in VB and maybe someone can help me out.

private void webBrowser1_Navigating(object sender,
                          WebBrowserNavigatingEventArgs e)
        {
            int index;
            string loc_url = "";
            string sid = "";
            System.Windows.Forms.HtmlDocument document =
                this.webBrowser1.Document;

            // e.Cancel = true;

            loc_url = e.Url.ToString();
            index = loc_url.IndexOf("&sid=", 0, loc_url.Length);

            if ((index > 15))
            {
                index = index + 5; // offset the &sid= 
                sid = loc_url.Substring(index, loc_url.Length - index);
                if (sid.Contains("session"))
                    return;

                XmlDocument doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode chunkDataNode = doc.CreateElement("ChunkData");
                doc.AppendChild(chunkDataNode);

                XmlNode fieldNode = doc.CreateElement("fields");
                chunkDataNode.AppendChild(fieldNode);

                XmlNode field6Node = doc.CreateElement("field6");
                field6Node.AppendChild(doc.CreateTextNode(sid));
                fieldNode.AppendChild(field6Node);

                string path = @"C:\Dictaphone";
                try
                {
                    // Determine whether the directory exists.
                    if (Directory.Exists(path))
                    {
                        doc.Save(path + @"\Study.xml");

                    }
                    else
                    {
                        // Try to create the directory.
                        DirectoryInfo di = Directory.CreateDirectory(path);
                        if (Directory.Exists(path))
                        {
                            doc.Save(path + @"\Study.xml");

                        }
                    }

                }
                catch (Exception exc)
                {
                    Console.WriteLine("The process failed: {0}", exc.ToString());
                }
                finally { }




            }


        }

is the C# code and it works.

Here is what I have in VB that doesn't work.

Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs)
        Dim index As Integer
        Dim loc_url As String = ""
        Dim sid As String = ""
        Dim document As HtmlDocument = Me.WebBrowser1.Document

        

        ' e.Cancel = true;

        loc_url = e.Url.ToString()
        index = loc_url.IndexOf("&sid=", 0, loc_url.Length)

        If (index > 15) Then
            index = index + 5 ' offset the &sid=
            sid = loc_url.Substring(index, loc_url.Length - index)
            If sid.Contains("session") Then
                Return
            End If
        End If
        Dim doc As New XmlDocument()
        Dim docNode As XmlNode = doc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
        doc.AppendChild(docNode)

        Dim chunkDataNode As XmlNode = doc.CreateElement("ChunkData")
        doc.AppendChild(chunkDataNode)

        Dim fieldNode As XmlNode = doc.CreateElement("fields")
        chunkDataNode.AppendChild(fieldNode)

        Dim field6Node As XmlNode = doc.CreateElement("field6")
        field6Node.AppendChild(doc.CreateTextNode(sid))
        fieldNode.AppendChild(field6Node)

        Dim path As String = "C:\Dictaphone"
        Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) Then
                doc.Save("C:Dictaphone\Study.xml")

            Else
                ' Try to create the directory.
                Dim di As DirectoryInfo = Directory.CreateDirectory(path)
                If Directory.Exists(path) Then
                    doc.Save("C:Dictaphone\Study.xml")

                End If
            End If

        Catch exc As Exception
            Console.WriteLine("The process failed: {0}", exc.ToString())
        Finally
        End Try

    End Sub

And last but not least, this is what the XML file should look like.

<?xml version="1.0" encoding="UTF-8" ?> 
- <ChunkData>
- <fields>
  <field6>188163</field6> 
  </fields>
  </ChunkData>

It is supposed to not only create the file but launch a program called PowerScribe and find the case in the Field 6 section.

Any help would be greatly appreciated.

doc.Save("C:Dictaphone\Study.xml")

this path does not exist! You forgot the \
do

doc.Save("C:\Dictaphone\Study.xml")

Edited 6 Years Ago by GeekByChoiCe: n/a

Didn't help any, thanks anyways. I think I made that error on my last code test because I did have it correct before

Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
		Dim loc_url As String = e.Url.ToString()
		Dim index As Integer = loc_url.IndexOf("&sid=", 0, loc_url.Length)
		Dim sid As String = ""
		Dim document As HtmlDocument = Me.WebBrowser1.Document

		e.Cancel = True

		If (index > 15) Then
			index = index + 5 ' offset the &sid=
			sid = loc_url.Substring(index, loc_url.Length - index)
			If sid.Contains("session") Then
				Return
			End If
		End If
		Dim doc As XDocument = <?xml version="1.0" encoding="UTF-8"?>
							   <ChunkData>
								   <fields>
									   <field6><%= sid %></field6>
								   </fields>
							   </ChunkData>

		Dim path As String = "C:\Dictaphone"
		Try
			' Determine whether the directory exists.
			If IO.Directory.Exists(path) Then
JUMP:
				doc.Save(IO.Path.Combine(path, "Study.xml"))
			Else
				' Try to create the directory.
				IO.Directory.CreateDirectory(path)
				GoTo JUMP
			End If

		Catch exc As Exception
			Debug.WriteLine("The process failed: {0}", exc.ToString())
		Finally
		End Try
		e.Cancel = False
	End Sub

Thanks Geek, waaaay closer. XML file not quite right but at least it's writing it now. Here is what I get.

<?xml version="1.0" encoding="utf-8" ?> 
- <ChunkData>
- <fields>
  <field6 /> 
  </fields>
  </ChunkData>

Should be

<?xml version="1.0" encoding="UTF-8" ?> 
- <ChunkData>
- <fields>
  <field6>188163</field6> 
  </fields>
  </ChunkData>

the filed6 node is empty because you only assign the "sid" when index > 15, else sid = ""

So you better add debug.writeline(sid) just before "Dim doc As XDocument =..."
Then you will see if sid is an empty string or not.

Ok, then there must be something wrong with the entire code. I guess that's what I get for using a convert program as it still isn't getting the sid and loading it into the XML file.

Ok i did a test with a static url (as i dont know which url you try)

Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated

Dim loc_url As String = "http://myTestPage.com/index.php?page=Thread&threadID=61780&sid=9bb1c793df61de929867eb57e24c77d15bc0aaa4" 
'Dim loc_url As String =  e.Url.ToString()
		Dim index As Integer = loc_url.IndexOf("&sid=", 0, loc_url.Length)
		Dim sid As String = ""

		If (index > 15) Then
			index = index + 5 ' offset the &sid=
			sid = loc_url.Substring(index, loc_url.Length - index)
			If sid.Contains("session") Then
				Debug.WriteLine("returning - session")
				Return
			End If
		End If

		If String.IsNullOrEmpty(sid) Then
			Debug.WriteLine("sid is nothing")
			Return
		End If

		'only write the xml if sid is NOT nothing
		Dim doc As XDocument = <?xml version="1.0" encoding="UTF-8"?>
							   <ChunkData>
								   <fields>
									   <field6><%= sid %></field6>
								   </fields>
							   </ChunkData>

		Dim path As String = "C:\Dictaphone"
		Try
			' Determine whether the directory exists.
			If IO.Directory.Exists(path) Then
JUMP:
				doc.Save(IO.Path.Combine(path, "Study.xml"))
			Else
				' Try to create the directory.
				IO.Directory.CreateDirectory(path)
				GoTo JUMP
			End If

		Catch exc As Exception
			Debug.WriteLine("The process failed: {0}", exc.ToString())
		End Try

You also should consider to handle WebBrowser1.Navigated instead of WebBrowser1.Navigating as in my example above

The example created the xml file with content:

<?xml version="1.0" encoding="utf-8"?>
<ChunkData>
  <fields>
    <field6>9bb1c793df61de929867eb57e24c77d15bc0aaa4</field6>
  </fields>
</ChunkData>
This article has been dead for over six months. Start a new discussion instead.