Selected Marked elements are not writing in new document - vb.net

This code was working and now it's not. I have no idea why. What the code is supposed to do is take all content after to and paste write it to a new file.
Right now it's grabbing all the text regardless of the start/stop markers
I've tried changing the insideBlock to true or false. I've also tried changing the source document to make sure the comment markers are right.
Here's my code
Dim startMark As String = "<!--#start#-->"
Dim stopMark As String = "<!--#stop#-->"
searchDir = txtDirectory.Text
Prefix = txtBxUnique.Text
For Each singleFile In allFiles
If File.Exists(singleFile.FullName) Then
Dim fileName = singleFile.FullName
Debug.Print("file name : " & fileName)
' A backup first
Dim backup As String = fileName & ".bak"
File.Copy(fileName, backup, True)
' Load lines from the source file in memory
Dim lines() As String = File.ReadAllLines(backup)
' Now re-create the source file and start writing lines inside a block
Dim insideBlock As Boolean = False
Using sw As StreamWriter = File.CreateText(backup)
For Each line As String In lines
If line = startMark Then
' start writing at the line below
insideBlock = True
ElseIf line = stopMark Then
' Stop writing
insideBlock = False
ElseIf insideBlock = True Then
' Write the current line in the block
sw.WriteLine(line)
End If
Next
End Using
End If
Next
Here's source data
<!--Arbortext, Inc., 1988-2010, v.4002-->
<!DOCTYPE DOC PUBLIC "-//USA-DOD//DTD 38784STD-BV7//EN" [
<!ENTITY graphic3-16_dgcs_file_window SYSTEM Graphics\3-16_dgcs_file_window.cgm" NDATA cgm>
<!ENTITY graphic19000_2 SYSTEM "Graphics\19000_2.cgm" NDATA cgm> ]>
<?Pub UDT _bookmark _target>
<?Pub EntList alpha bull copy rArr sect trade deg>
<?Pub Inc>
<doc service="af" docid="TO 1Q-1(M)B-2-2-12-1" docstat="formal" verstatpg="ver" cycle="2" chglevel="1">
<front numcols="1">
<idinfo>
<tmidno></tmidno>
<chgnum></chgnum>
<chgdate></chgdate>
<chghistory>
<chginfo>
<chgtxt></chgtxt>
<date></date></chginfo></chghistory>
<doctype></doctype>
<maintlvl></maintlvl>
<!--#start#-->
<prtitle>
<subject>
Trying to get this code to work</subject></prtitle>
<mfr></mfr>
<contractno>A12345</contractno>
<contractno>B12</contractno>
<!--#start#-->
<line></line>
<contractno>Contract No</contractno>
<supersed></supersed>
<discl>Discipline</discl>
<distrib>
<emphasis type="u"></emphasis></distrib>
<expcont></expcont>
<destr>Destruction</destr>
<authnot></authnot>
<pubdate></pubdate></idinfo>
<lep>
<lepcontents autobuild="1"></lep>
<contents autobuild="1">
<illuslist autobuild="1">
<tablelist autobuild="1">
<foreword>
<!--#start#-->
<para0 verstatus="ver">
<title></title>
<para>
This text should be in the new file</para></para0>
<para0 verstatus="ver">
<title></title>
<para>
<!--#stop#-->
<acronym>
<def></def>
<term></term></acronym></para></para0>
<?Pub Caret -2></foreword>
<safesum>
<para0 verdate="5/2/16" verstatus="ver">
<title>
</title>
<para>
</para>
</para0></safesum></front>
<body numcols="1">
<chapter id="chap1">
<title>Chapter 1</title>
<para0 verstatus="ver">
<title>Paragraph 1</title>
<para></para></para0></chapter>
<chapter id="chap2">
<title>Chapter 2</title>
<section id="thoery_of_operation_section">
<title></title>
<para0 verstatus="ver">
<title>Paragraph 2</title>
<para>
<!--#start#-->
<change level="1" change="delete">
<emphasis type="u" color="blue">
<xref xrefid="fig2-1">Figure 1 Smelly Fish</emphasis></change>
<!--#stop#-->
</para></para0></section>
<section>
<title></title>
<para0>
<title></title></para0></section></chapter>
</body>
</doc>
The results should be just the elements in between the markers.
<prtitle>
<subject>
Trying to get this code to work</subject></prtitle>
<mfr></mfr>
<contractno>A12345</contractno>
<contractno>B12</contractno>
<para0 verstatus="ver">
<title>Para0 Title</title>
<para>
This text should be in the new file</para></para0>
<para0 verstatus="ver">
<title></title>
<para>
<acronym>
<def>definition</def>
<term>Look here</term></acronym></para>
<change level="1" change="delete">
<emphasis type="u" color="blue">
<xref xrefid="fig2-1">Figure 1 Smelly Fish</emphasis></change>
Thank you for all the help,
Max

Related

Selenium VB.Net 2019 Save canvas image

Using the Code given at this link by mehmet mecek
I am attempting to Save the Image of Tag using Selenium VB.net 2019. My code for this action is
Dim ele = driver.FindElementByClassName("canvasWrapper")
Dim base64string = Trycast(driver.ExecuteScript("
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = document.getElementById('page1');
c.height=img.naturalHeight;
c.width=img.naturalWidth;
ctx.drawImage(img, 0, 0,img.naturalWidth, img.naturalHeight);
var base64String = c.toDataURL();
return base64String;
"), String)
MsgBox(base64string.ToString())
Dim base64 = base64string.Split(",").Last()
Dim stream = New MemoryStream(Convert.FromBase64String(base64))
Dim bmCaptured As Bitmap = New Bitmap(stream)
bmCaptured.Save(Path + "\" + pageNum.ToString + ".jpg", Imaging.ImageFormat.Jpeg)
I am getting error `The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. I have tried With and Without Trycast but am not able to save the canvas but some errors keep cropping up.
VB.Net Code - Got it... But trying for More Workarounds
Dim bmpScreen As Screenshot = driver.GetScreenshot()
Dim scrshot As String= bmpScreen.AsBase64EncodedString
Dim screenshotAsByteArray As Byte() = bmpScreen.AsByteArray
bmpScreen.SaveAsFile(Path + "\" + pageNum.ToString + ".jpg",
System.Drawing.Imaging.ImageFormat.Jpeg)
After this, we can load the image again and crop to element size.
Another and the Best Method code snippet I could search and develop is as follows and is working nicely
Dim fileName = pageNum.ToString + ".png"
Dim jsExeString As String = "var element = document.createElement('a');
element.setAttribute('href',
document.getElementById('page1').toDataURL('image/png'));
element.setAttribute('download', '" + fileName + "');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);"
driver.ExecuteScript(jsExeString)
pagenum is basically a Counter used as File name for each saved image. Only drawback in this is that all files are downloaded to Default Download Folder.
Third Method, I could Use is
Dim ele = driver.FindElementById("mainContainer")
Dim stringForFile = "<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta charset='UTF-8'/>
<meta name='viewport' content='width=device-width; initial-scale=1.0; user-scalable=no'/>
<meta content='notranslate' name='google'/>
<meta http-equiv='X-UA-Compatible' content='IE=edge'/>
<link rel='stylesheet' href='.\viewer.css' type='text/css'>
<link rel='stylesheet' href='.\book.css' type='text/css'>
<link rel='stylesheet' href='.\stick.css' type='text/css'>
<link rel='stylesheet' href='.\template.css' type='text/css'>
<link rel='stylesheet' href='.\media-queries.css' type='text/css'>
<link rel='stylesheet' href='.\styles.css' type='text/css'>
<link rel='stylesheet' href='.\jquery-ui.css' type='text/css'>
<link rel='stylesheet' type='text/css' href='.\jquery.fancybox.css' media='screen' />
<script src='.\jspdf.js'></script>
<script src='.\from_html.js'></script>
<script src='.\split_text_to_size.js'></script>
<script src='.\standard_fonts_metrics.js'></script>
</head><body>" &
ele.GetAttribute("innerHTML") &
"</body></html>"
File.WriteAllText(Path + "\" + pageNum.ToString + ".htm", stringForFile)
All the .css and .js files can be copied to the Path (variable defining the the target folder) using statement like this
FileCopy(My.Application.Info.DirectoryPath & "\css\viewer.css", Path & "\" & "viewer.css")

Save vb.net HTML in UTF8 or Unicode

Hello and good afternoon, i am in development of my project long story short i need to save my html base page with UTF8 or Unicode
Dim y As String
Dim UTF8encoding() As Byte, MyEncoder As New System.Text.UTF8Encoding(TRUE)
y = (html code should be here will be under)
Dim utf8 As New UTF8Encoding()
Dim utf8EmitBOM As New UTF8Encoding(True)
Dim code As String = y
Path = "C:\Users\OWNER\Desktop\invoice.html"
Try
Dim my_write As System.IO.StreamWriter
my_write = IO.File.CreateText(path)
my_write.write(utf8EmitBOM.GetPreamble())
UTF8encoding = System.Text.Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.Unicode, MyEncoder.GetBytes(y))
my_write.WriteLine(code)
my_write.Close()
Catch ex As Exception
End Try
HTML
<html>
<style>
table, th, td
{ border: 1px solid black; }
</style>
</head>
<body><center><b>
<font size=20>Family Butcher</font></br></br></br><center><b>
<font size =4>164 Battersea Bridge Road London SW11 3AW</center></font><center><b>
<font size =4>Tel: Mob:</center></font><center><b>
<font size =4>VAT No: 835522334</center></br></font></br>
<table Border = 3 WIDTH=610 align=left></br><tr>
<th colspan=3 align = left>To: " & txto & " <br/>
<br/>Date: <br/><br/>Invoice nº <br/></th></tr>
<td WIDTH = 100 HEIGHT=40><center><b>Quantity </b></td>
<td WIDTH = 400><center><b>Description</b></td><td><center>
<b>Value</b> </br></p>
</body></html>
At the moment i do not know how to save the file in either unicode or utf8 and then i cannot open the html file without the symbols "Â" Thanks for any support
I suggest you use File.WriteAllText - this overload allows you to specify encoding, so you can accomplish your goal with just one line of code, for example:
File.WriteAllText(path, code, Encoding.UTF8)

serializing soap response in vb.net

Using response As WebResponse = request.GetResponse()
Using rd As New StreamReader(response.GetResponseStream())
Dim soapResult As String = rd.ReadToEnd()
SerializeCollection(soapResult)
the soapResult generate the following :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<createShipmentResponse xmlns="http://www.royalmailgroup.com/api/ship/V2">
<integrationHeader>
<dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1">2016-03-23T06:55:32</dateTime>
<version xmlns="http://www.royalmailgroup.com/integration/core/V1">2</version>
<identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
<applicationId>RMG-API-G-01</applicationId>
<transactionId>730222611</transactionId>
</identification>
</integrationHeader>
<integrationFooter>
<errors xmlns="http://www.royalmailgroup.com/integration/core/V1">
<error>
<errorCode>E1105</errorCode>
<errorDescription>The countryCode specified is not valid</errorDescription>
</error>
<error>
<errorCode>E1001</errorCode>
<errorDescription>Postcode AA9 0AA invalid</errorDescription>
</error>
</errors>
<warnings xmlns="http://www.royalmailgroup.com/integration/core/V1">
<warning>
<warningCode>W0036</warningCode>
<warningDescription>E-mail option not selected so e-mail address will be ignored</warningDescription>
</warning>
<warning>
<warningCode>W0035</warningCode>
<warningDescription>SMS option not selected so Telephone Number will be ignored</warningDescription>
</warning>
</warnings>
</integrationFooter>
</createShipmentResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
i am using this method to serialize this response :
Private Sub SerializeCollection(filename As String)
Dim Emps As createShipmentResponse = New createShipmentResponse()
' Note that only the collection is serialized -- not the
' CollectionName or any other public property of the class.
Dim x As XmlSerializer = New XmlSerializer(GetType(createShipmentResponse))
Dim writer As TextWriter = New StreamWriter(filename)
x.Serialize(writer, Emps)
writer.Close()
End Sub
but i am having this error : Illegal characters in path
what does that mean ? and how can i fix this ?
Your SerializeCollection() method expects a file path to serialize to a file. You're currently passing the contents of your response which is why it doesn't work.
If you haven't written the method yourself I think you've not found completely what you're looking for.
This is an example of how the method should be called:
SerializeCollection("C:\Users\Vincent\Desktop\Hello.bin") 'The extension doesn't need to be '.bin', but it's an example.
The method has currently no way of getting your response, for that you should add a second parameter.
As I'm not familiar with Soap serialization I'm afraid I cannot help you further.

How to extract individual/child nodes from a KML file in VisualBasic?

I need to be able to extract individual nodes from this file into variables for further manipulation. I'm writing to the console to see what information is being pulled, but I am struggling to pull the name or description.
I can successfully print the entire file. I've tried getting individual nodes using placemark.<name>.Value and placemark.Element("name").Value, the second of which throws a NullReferenceException. Any ideas on how to be able to pull out the name and description in this instance?
Imports System.Xml
Imports System.Xml.Linq 'Visual Studio 2015 tells me this isn't needed
Imports System.Core 'Visual Studio 2015 tells me this isn't needed
Dim file As XDocument = XDocument.Load(filePath)
Dim placemarks As IEnumerable(Of XElement) = From test In file.Root.Elements()
For Each placemark As XElement In placemarks
Console.WriteLine(placemark) 'This works
Console.WriteLine(placemark.<name>.Value) 'This prints an empty line
Console.WriteLine(placemark.Element("description").Value) 'This throws a NullReferenceException
Next
This is the structure
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Document>
<name>Untitled layer</name>
<Placemark>
<name>Name 1</name>
<description>Description 1</description>
<ExtendedData>
<Data name='Test data one'>
<value>Test data 1</value>
</Data>
</ExtendedData>
<Point>
<coordinates>34725567547</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Name 2</name>
<description>Description 2</description>
<ExtendedData>
<Data name='Test data two'>
<value>Test data 2</value>
</Data>
</ExtendedData>
<Point>
<coordinates>056795763767</coordinates>
</Point>
</Placemark>
If I have understood you correctly, you are trying to fetch the name & description present inside the PlaceMark node. But, since you are only fetching Root.Elements() your query will only fetch the complete XML starting from your root node.
You need to find the Descendants of PlaceMark node because you need to fetch the name & description inside it. Also, since the root node kml consists of namespace you need to specify that as well.
Here is the code:-
Dim ns As XNamespace = "http://www.opengis.net/kml/2.2"
Dim placeMarks = From test In file.Root.Element(ns + "Document")
.Descendants(ns + "Placemark") Select test
For Each pm In placeMarks
Console.WriteLine("Name: {0}", pm.Element(ns + "name").Value)
Console.WriteLine("Description: {0}", pm.Element(ns + "description").Value)
Console.WriteLine()
Next
I am getting following output:-

vb.Net: How can I read a large XML file quickly?

I am trying to read this XML document.
An excerpt:
<datafile xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="wiitdb.xsd">
<WiiTDB version="20100217113738" games="2368"/>
<game name="Help Wanted: 50 Wacky Jobs (DEMO) (USA) (EN)">
<id>DHKE18</id>
<type/>
<region>NTSC-U</region>
<languages>EN</languages>
<locale lang="EN">
<title>Help Wanted: 50 Wacky Jobs (DEMO)</title>
<synopsis/>
</locale>
<developer>HUDSON SOFT CO., LTD.</developer>
<publisher>Hudson Entertainment, Inc.</publisher>
<date year="2009" month="" day=""/>
<genre>party</genre>
<rating type="ESRB" value="E10+">
<descriptor>comic mischief</descriptor>
<descriptor>mild cartoon violence</descriptor>
<descriptor>mild suggestive themes</descriptor>
</rating>
<wi-fi players="0"/>
<input players="2">
<control type="wiimote" required="true"/>
<control type="nunchuk" required="true"/>
</input>
<rom version="" name="Help Wanted: 50 Wacky Jobs (DEMO) (USA) (EN).iso" size="4699979776"/>
</game>
So far I have this:
Dim doc as XPathDocument
Dim nav as XPathNavigator
Dim iter as XPathNodeIterator
Dim lstNav As XPathNavigator
Dim iterNews As XPathNodeIterator
doc = New XPathDocument("wiitdb.xml")
nav = doc.CreateNavigator
iter = nav.Select("/WiiTDB/game") 'Your node name goes here
'Loop through the records in that node
While iter.MoveNext
'Get the data we need from the node
lstNav = iter.Current
iterNews = lstNav.SelectDescendants(XPathNodeType.Element, False)
'Loop through the child nodes
txtOutput.Text = txtOutput.Text & vbNewLine & iterNews.Current.Name & ": " & iterNews.Current.Value
End While
It just skips the "While iter.MoveNext" part of the code. I tries it with a simple XML file, and it works fine.
I think your XPath query is off. WiiTDB is a closed node, so you need to look for /datafile/game or //game.
Use the System.Xml.Serialization namespace instead: create a dedicated, serializable class to hold the data you wish to load and define shared serialize / deserialize functions with strongly typed arguments to do the work for you.
As the structure of the new classes will closely follow that of your XML data, there should be no confusion as to which data is located where within a run time instance.
See my answer here for an idea of how to create a class from an example XML file.