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
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()
My XML File:
<?xml version="1.0" encoding="utf-8"?>
<DisksLibrary>
<Disk>
<DiskName>Test</DiskName>
<DiskPath>testpath</DiskPath>
</Disk>
<Disk>
<DiskName>Test1</DiskName>
<DiskPath>testpath1</DiskPath>
</Disk>
<Disk>
<DiskName>Test2</DiskName>
<DiskPath>testpath2</DiskPath>
</Disk>
</DisksLibrary>
I was wondering since I have multiple tags with the same name such as and in My XML File, is it possible to retrieve a particular set of data from the XML File? For example, if I want to retrieve the DiskPath innertext which is under the same set as the one with the InnerText of "Test". In other words, the DiskPath I should retrieve is "testpath1". How do I ensure it does not confuse with other DiskPath innertext?
I tried searching online but either no solution was provided or the solution provided does not work for me. I found one solution which seem to work but after reading the code I don't think it works if there are multiple tags with the same name.
The solution I found which I am not sure if it resolves my problem is:
Dim document As XmlReader = New XmlTextReader("MyXML.xml")
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
If (document.Name = "FirstName") Then
TextBox1.Text = document.ReadInnerXml.ToString()
End If
If (document.Name = "LastName") Then
TextBox2.Text = document.ReadInnerXml.ToString()
End If
End If
End While
Thank you for your help. Appreciate it.
Reading about XPath and Predicates (http://www.w3schools.com/xpath/xpath_syntax.asp) would be very useful to you. In your instance you want to return the inner text of any "DiskPath" elements whose sibling "DiskName" has a value of "Test". As Styxxy described the XPath /DisksLibrary/Disk[DiskName="Test"]/DiskPath would achieve this:
Dim doc As New XmlDocument
Dim targetPath As String
doc.Load("MyXML.xml")
Dim foundNode As XmlNode = doc.SelectSingleNode("/DisksLibrary/Disk[DiskName='Test']/DiskPath")
If Not (foundNode Is Nothing) Then
targetPath = foundNode.InnerText
End If
I would consider parameterizing the XPath approach to make it scalable.
Imports System.Xml
Module Module1
Sub Main()
Dim xml As String = _
"<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<DisksLibrary>" & _
" <Disk>" & _
" <DiskName>Test1</DiskName>" & _
" <DiskPath>testpath1</DiskPath>" & _
" </Disk>" & _
" <Disk>" & _
" <DiskName>Test2</DiskName>" & _
" <DiskPath>testpath2</DiskPath>" & _
" </Disk>" & _
" <Disk>" & _
" <DiskName>Test3</DiskName>" & _
" <DiskPath>testpath3</DiskPath>" & _
" </Disk>" & _
"</DisksLibrary>"
Dim doc As New XmlDocument
doc.LoadXml(xml)
For i = 1 To 4
Dim foundNode = FindSiblingNode(doc, "Test" & i, "DiskPath")
Console.WriteLine(If(Not foundNode Is Nothing, foundNode.InnerText, "Node not found"))
Next
Console.ReadLine()
End Sub
Public Function FindSiblingNode(ByVal doc As XmlDocument, _
ByVal siblingNodeInnerText As String, _
ByVal searchNodeName As String) As XmlNode
Return doc.SelectSingleNode(String.Format("/DisksLibrary/Disk[DiskName='{0}']/{1}", siblingNodeInnerText, searchNodeName))
End Function
End Module
Results:
testpath1
testpath2
testpath3
Node not found
Here's another option using LINQ to XML and XML literals:
Dim document = XDocument.Load("MyXML.xml")
Dim path As String = (
From disk In document...<Disk>
Where disk.<DiskName>.Value = "Test"
Select disk.<DiskPath>.Value
).SingleOrDefault()
path will either be the value of the matching <DiskPath> element, or Nothing if not found.
dear all im tried to send multiline sms using gnikii but it fails
Dim xCmd As String
xCmd = "cmd.exe /c echo " & txtBody.Text & " | c:\sms\gnokii.exe --sendsms 0771234567 2> test.txt"
Shell(xCmd)
Please help me
You can split your text on vbCrLf and add echo in front so you end up with this cmdline instead
cmd.exe /c lineOne echo line2 | c:\sms\gnokii.exe --sendsms 0771234567 2> test.txt
code wise you can do it like this:
Dim sString As String
Dim aLines As String()
Dim xCmd As String
sString = "lineOne" & vbCrLf & "line2"
aLines = sString.Split(vbCrLf)
sString = Join(aLines," echo")
xCmd = "cmd.exe /c " & sString & " | c:\sms\gnokii.exe --sendsms 0771234567 2> test.txt"
Shell(xCmd)
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