My xml document looks like .. (its actually a kml file for google map..)
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Folder>
<Name>Folder1</Name>
<Placemark>
<Name>Placemark1Folder1</Name>
<LookAt>
<longitude>-122.0839597145766</longitude>
<latitude>37.42222904525232</latitude>
</LookAt>
</Placemark>
<Placemark>
<Name>Placemark2Folder1</Name>
<LookAt>
<longitude>-101.083959</longitude>
<latitude>26.422</latitude>
</LookAt>
</Placemark>
</Folder>
<Folder>
<Name>Folder2</Name>
<Placemark>
<Name>Placemark1Folder2</Name>
<LookAt>
<longitude>-96.566556</longitude>
<latitude>14.422</latitude>
</LookAt>
</Placemark>
</Folder>
</Document>
</kml>
According to each Placemark for each folder node i will like to make a separate xml file like
1st XML:
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Folder>
<Name>Folder1</Name>
<Placemark>
<Name>Placemark1Folder1</Name>
<LookAt>
<longitude>-122.0839597145766</longitude>
<latitude>37.42222904525232</latitude>
</LookAt>
</Placemark>
</Folder>
</Document>
</kml>
2nd xml
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Folder>
<Name>Folder1</Name>
<Placemark>
<Name>Placemark2Folder1</Name>
<LookAt>
<longitude>-101.083959</longitude>
<latitude>26.422</latitude>
</LookAt>
</Placemark>
</Folder>
</Document>
</kml>
3rd xml
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Folder>
<Name>Folder2</Name>
<Placemark>
<Name>Placemark1Folder2</Name>
<LookAt>
<longitude>-96.566556</longitude>
<latitude>14.422</latitude>
</LookAt>
</Placemark>
</Folder>
</Document>
</kml>
..i am very beginner in xml ..help please
Between <Placemark> and </Placemark> tag ..
dim strXML as string = .... 'place your XML to be splitted here
dim x as integer
Dim aXML As New List(Of String)
dim sAdd1 as String = '<kml xmlns="http://www.opengis.net/kml/2.2"><Document><Folder> <Name>Folder1</Name><Placemark>'
dim sAdd2 as String = '</Placemark></Folder></Document></kml>'
while true
x=instr(strXML,"<Placemark>")
if x > 0 then
strXML = mid(strXML,x+11)
x=instr(strXML,"</Placemark>")
aXML.Add(sAdd1 & mid(strXML,1,x-1) & sAdd2)
strXML = mid(strXML,x+12)
strXML = trim(strXML)
if strXML.length=0 then exit while
else
exit while
endif
loop
aXML is result array ..
The code not tested yet .. so, let me know if that's not working ..
Finally i made success for splitting xml according to node ..I have saved individual kml files according to node in xml ..Here is my solution
Public Sub SplitXml(ByVal XmlDoc As XmlDocument, ByVal SaveLocation As String)
Dim TmpXml As XmlDocument = XmlDoc
Dim Str As String = "<?xml version=""1.0"" encoding=""UTF-8""?>" & "<kml xmlns=" & Chr(34) & "http://www.opengis.net/kml/2.2" & Chr(34) & ">" & "<Document>"
Dim DocumentNodes As XmlNodeList = TmpXml.GetElementsByTagName("Document")
'=======================
'Building Common String
'=======================
For Each node As XmlNode In DocumentNodes
Dim DocumentChildNodes As XmlNodeList = node.ChildNodes
For Each Childnode As XmlNode In DocumentChildNodes
If Childnode.Name <> "Folder" Then
Str = Str & Childnode.OuterXml.Replace("xmlns=""http://www.opengis.net/kml/2.2""", "")
Else
Exit For
End If
Next
Next
Dim FolderNodes As XmlNodeList = TmpXml.GetElementsByTagName("Folder")
Dim FolderName As String = String.Empty
Dim XmlDocSave As XmlDocument = New XmlDocument()
Dim StrXml As String = String.Empty
Dim TmpStr As String = String.Empty
Dim FileName As String = String.Empty
For Each node As XmlNode In FolderNodes
'==============================================================
'Creating Directories For kml Getting Name from FirstChild Node
'===============================================================
FolderName = DirectCast(DirectCast(node, System.Xml.XmlElement).FirstChild, System.Xml.XmlElement).InnerText
FolderName = FolderName.Replace(".", "_")
FolderName = FolderName.Replace(" ", "")
If (Not System.IO.Directory.Exists(SaveLocation & "\" & FolderName)) Then
System.IO.Directory.CreateDirectory(SaveLocation & "\" & FolderName)
End If
'==============================================================
'Creating kml Files Getting Name from FirstChild Node
'===============================================================
Dim FolderChildNodes As XmlNodeList = node.ChildNodes
For Each childnode As XmlNode In FolderChildNodes
If childnode.Name = "Placemark" Then
FileName = DirectCast(DirectCast(childnode, System.Xml.XmlElement).FirstChild, System.Xml.XmlElement).InnerText
FileName = FileName.Replace(".", "_")
FileName = FileName.Replace(" ", "")
StrXml = Str & "<Folder>" & TmpStr & childnode.OuterXml & "</Folder>" & "</Document>" & "</kml>"
XmlDocSave.LoadXml(StrXml)
XmlDocSave.Save(SaveLocation & "\" & FolderName & "\" & FileName & ".kml")
XmlDocSave = New XmlDocument()
StrXml = String.Empty
Else
TmpStr = TmpStr & childnode.OuterXml
End If
Next
TmpStr = String.Empty
Next
End Sub
Related
I'm using this code to save files in my app
Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
So now I have a textbox1 and I want to show the path of last saved image in it
how?
Regards,,,,
What I've done in the past is generate the path in one step and then use the generated variable to do the saving and to display.
So instead of:
Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
Try:
'Generate the Path
Dim path As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now))
'Save using the generated path
PictureBox1.Image.Save(path)
'Display the path
textbox1.Text = path
Thanks all I've done it successfully `
Dim filename As String = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
Dim filePath1 = (IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filename)))
Dim filePath2 = (IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), ("RMSS")))
If IO.Directory.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (" RMSS"))) = True Then
TextBox1.Text = filePath1
TextBox2.Text = filePath2 & "\" & filename
PictureBox1.Image.Save(filePath1)
My.Computer.FileSystem.MoveFile(TextBox1.Text, TextBox2.Text, True)
Else
TextBox1.Text = filePath1
TextBox2.Text = filePath2 & "\" & filename
PictureBox1.Image.Save(filePath1)
My.Computer.FileSystem.MoveFile(TextBox1.Text, TextBox2.Text, True)
End If
I have multiple large XML files and am trying to extract 5 instances of a specific element and its children. I have the code all set, however, I HAVE to use StreamWriter to write out the xml. How can I do this so that it comes out properly indented, etc.
The string looks similar to this:
<SampleMAIN><Sample type="1"><Sample_Batch>123
</Sample_Batch><SampleMethod>
</SampleMethod>
</Sample></SampleMAIN>
I want it to look like this:
<SampleMAIN>
<Sample type="1">
<Sample_Batch>123
</Sample_Batch>
<SampleMethod>1
</SampleMethod>
</SampleMAIN>
With using StreamWriter, the below code will output the format that you need and append to an existing xml file.
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
Dim sw As System.IO.StreamWriter
Dim St As String = "1"
Dim Sb As String = "123"
Dim Sm As String = "1"
sw = File.AppendText("C:\XML_Files\sampler_02.xml")
sw.WriteLine("<SampleMAIN>")
sw.WriteLine(" <Sample type=" & """" & St & """" & ">")
sw.WriteLine(" <Sample_Batch>" & Sb)
sw.WriteLine(" </Sample_Batch>")
sw.WriteLine(" <SampleMethod>" & Sm)
sw.WriteLine(" </SampleMethod>")
sw.WriteLine("</SampleMAIN>")
sw.Close()
End Sub
So for anyone who may come across this and was interested in how I resolved it, here is what I used...
Dim dir As New DirectoryInfo("D:\data")
Dim sw As New StreamWriter("C:\Documents\largeFile.xml")
Dim xd As New XmlDocument
Dim iCount As Integer
sw.WriteLine("<?xml version=""1.0"" encoding=""ISO-8859-1""?>" & vbCrLf & "<Root>")
For Each fi As FileInfo In dir.GetFiles()
xd.Load(fi.FullName)
iCount = 0
For Each xn As XmlNode In xd.SelectNodes("//Root")
For Each xe As XmlElement In xn.ChildNodes
iCount += 1
sw.WriteLine(xe.OuterXml.ToString)
If iCount = 5 Then Exit For
Next
Exit For
Next
Next
sw.WriteLine("</Root>")
sw.Flush() : sw.Close() : sw.Dispose()
which is the best way to parse this xml ???
<?xml version="1.0" encoding="UTF-8"?>
<DOCWARE Version="1.1">
<Order Count="2">
<Pos0>
<M_TEXTNR>sAuspuffendrohr</M_TEXTNR>
<VM_BESTNR>s02010000373</VM_BESTNR>
<P_PREIS>s715.80</P_PREIS>
</Pos0>
<Pos1>
<M_TEXTNR>sMutter</M_TEXTNR>
<VM_BESTNR>s02010000373</VM_BESTNR>
<P_PREIS>s0.70</P_PREIS>
</Pos1>
</Order>
<TotalSum>s</TotalSum>
</DOCWARE>
I Try to do so, but is not working then i = 1
Dim xmldoc As New XmlDataDocument()
Dim xmlnode As XmlNodeList
Dim fs As New FileStream(myFileName, FileMode.Open, FileAccess.Read)
xmldoc.Load(fs)
For i As Integer = 0 To 1
xmlnode = xmldoc.GetElementsByTagName("Pos" & i)
dim val as string = xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
MsgBox(val)
Next
thanks in advance
To read a tag with a changed name you can to do using xmlReader and find if you document.Name contains this prefix "Pos", try to use this code:
Dim document As XmlReader = New XmlTextReader(fileName)
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
If (document.Name.Contains("Pos")) Then
Dim test As String = document.ReadInnerXml.ToString()
Dim doc As New XmlDocument()
doc.LoadXml("<Pos>" & test & "</Pos>")
Dim root As XmlNode = doc.FirstChild
If root.HasChildNodes Then
dim _value = root.ChildNodes.Item(0).InnerText.Trim()
End If
End If
End If
End While
I have following xml Format,which is actually a kml file for google map..
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Name>TestDoc</Name>
<Style id="Style1">
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<Folder>
<Name>Folder1</Name>
<Placemark>
<Name>Placemark1Folder1</Name>
<LookAt>
<longitude>-122.0839597145766</longitude>
<latitude>37.42222904525232</latitude>
</LookAt>
</Placemark>
<Placemark>
<Name>Placemark2Folder1</Name>
<LookAt>
<longitude>-101.083959</longitude>
<latitude>26.422</latitude>
</LookAt>
</Placemark>
</Folder>
<Folder>
<Name>Folder2</Name>
<Placemark>
<Name>Placemark1Folder2</Name>
<LookAt>
<longitude>-96.566556</longitude>
<latitude>14.422</latitude>
</LookAt>
</Placemark>
</Folder>
</Document>
</kml>
I want to concat xml to string variable untill it finds <Folder> node
Hence output string will be :
""<kml xmlns="http://www.opengis.net/kml/2.2"><Document><Name>TestDoc</Name><Style id="Style1"><PolyStyle><fill>0</fill></PolyStyle> </Style>"
I am new to xml ..Plz help
Ok i got it ...here is my solution
Public Sub ConcatXmlToString()
Dim xmldoc As XmlDocument = New XmlDocument()
'================================
'Hard Coded
'================================
Dim ConcatString As String = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?><kml xmlns=" & Chr(34) & "http://www.opengis.net/kml/2.2" & Chr(34) & ">"
xmldoc.Load("E:\A01c.kml")
Dim DocumentNodeList As XmlNodeList = xmldoc.GetElementsByTagName("Document")
For Each DocumentNode As XmlNode In DocumentNodeList
'xmldoc.ParentNode.ParentNode.RemoveChild(childnode)
Dim ChildNodeList As XmlNodeList = DocumentNode.ChildNodes
For Each ChildNode As XmlNode In ChildNodeList
If ChildNode.Name <> "Folder" Then
ConcatString = ConcatString & ChildNode.OuterXml.Replace("xmlns=""http://www.opengis.net/kml/2.2""", "")
End If
Next
Next
ConcatString = ConcatString & "</Document></kml>"
Dim str As String = xmldoc.InnerText
End Sub
I have a checked list box and a thumbnail area to display them where I am trying to load only images from a specific folder and need to display in thumbnails area but the problem is there is a thumbs.db file which is also being added to the checked list box which I don't need it.
So how do I actually load only the image files without the thumbs.db file.
Here is my code:
Private Sub LoadProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadProjectToolStripMenuItem.Click
Using ofdlg As New Windows.Forms.OpenFileDialog
ofdlg.DefaultExt = "trk"
ofdlg.Filter = "Project|*.trk"
ofdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim SaveData As New gCanvasData
Using objStreamReader As New StreamReader(ofdlg.FileName)
Dim x As New XmlSerializer(GetType(gCanvasData))
SaveData = CType(x.Deserialize(objStreamReader), gCanvasData)
objStreamReader.Close()
End Using
With SaveData
'gTSSizer_gAZoom.Value = 100
GCanvas1.ImageXYReset()
GCanvas1.Image = .Image
GCanvas1.gAnnotates = .gAnnotates
GCanvas1.RebuildAll()
GCanvas1.AssembleBitmap()
End With
Dim fullpath As String
fullpath = Application.StartupPath + "\" & System.IO.Path.GetFileNameWithoutExtension(ofdlg.FileName) + "\"
For Each fi As FileInfo In New DirectoryInfo(fullpath).GetFiles
CheckedListBox1.Items.Add(Application.StartupPath + "\" & System.IO.Path.GetFullPath(ofdlg.FileName))
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, True)
ThumbControl1.AddFolder(fullpath, True)
Next i
Next
End If
End Using
End Sub
Either filter it inside of the For Each Loop:
For Each fi As FileInfo In New DirectoryInfo(fullpath).GetFiles
If Not {".jpg", ".png", ".bmp"}.Contains(fi.Extension) Then Continue For
' ...
Next
or do it in the GetFiles:
DirectoryInfo(fullpath).GetFiles(".jpg")
Found the solution at last:
Dim fullpath As String
fullpath = Application.StartupPath & "\" & System.IO.Path.GetFileNameWithoutExtension(ofdlg.FileName) + "\"
Dim FileDirectory As New IO.DirectoryInfo(fullpath)
Dim FileJpg As IO.FileInfo() = FileDirectory.GetFiles("*.jpg")
Dim FileGif As IO.FileInfo() = FileDirectory.GetFiles("*.gif")
Dim FileBmp As IO.FileInfo() = FileDirectory.GetFiles("*.bmp")
For Each File As IO.FileInfo In FileJpg
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
Next
For Each File As IO.FileInfo In FileGif
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
Next
For Each File As IO.FileInfo In FileBmp
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
Next
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, True)
Next i
Change DirectoryInfo(fullpath).GetFiles to DirectoryInfo(fullpath).EnumerateFiles() And add a search pattern for the image file extensions you want. http://msdn.microsoft.com/en-us/library/dd383574.aspx