How to extract individual/child nodes from a KML file in VisualBasic? - vb.net

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:-

Related

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.

Parsing XML response in VBA

I am calling a webservice from Excel macro but not able to parse the response XML. The Structure of the Response is as follows -
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:typ="http://xmlns.oracle.com/oracle/apps/marketing/commonMarketing/mktImport/model/types/">
<env:Header>
<wsa:Action>http://xmlns.oracle.com/oracle/apps/marketing/commonMarketing/mktImport/model//ImportPublicService/getImportActivityLogsResponse</wsa:Action>
<wsa:MessageID>urn:uuid:bffbffe9-a379-46bb-8205-7f6b5d15a0f5</wsa:MessageID>
</env:Header>
<env:Body>
<ns0:getImportActivityLogsResponse xmlns:ns0="http://xmlns.oracle.com/oracle/apps/marketing/commonMarketing/mktImport/model/types/">
<ns2:result xsi:type="ns1:ImportServiceLogs" xmlns:ns2="http://xmlns.oracle.com/oracle/apps/marketing/commonMarketing/mktImport/model/types/" xmlns:ns1="http://xmlns.oracle.com/oracle/apps/marketing/commonMarketing/mktImport/model/" xmlns:ns0="http://xmlns.oracle.com/adf/svc/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:LogFileName>log-test_contact_01-02_20150422171310_pverma Data File.csv</ns1:LogFileName>
<ns1:LogFileContent>
<xop:Include href="cid:ec277fd6-76e2-4ae3-9e8b-1b14c446adf8" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
</ns1:LogFileContent>
<ns1:ErrorLogFileName>error-test_contact_01-02_20150422171310_pverma Data File.csv</ns1:ErrorLogFileName>
<ns1:ErrorLogFileContent>
<xop:Include href="cid:d41bacf8-3134-4cd5-80b7-5a68c2b271ed" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
</ns1:ErrorLogFileContent>
<ns1:ExceptionLogFileName>exception-test_contact_01-02_20150422171310_pverma Data File.csv</ns1:ExceptionLogFileName>
<ns1:ExceptionLogFileContent>
<xop:Include href="cid:4aa55e1b-1d4d-48be-bdd0-66200de496ee" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
</ns1:ExceptionLogFileContent>
<ns1:BatchId xsi:nil="true"/>
</ns2:result>
</ns0:getImportActivityLogsResponse>
</env:Body>
</env:Envelope>
This response contains the flat files. I have to parse these files from above XML.
Please suggest any solution over it. Help will be much appreciated.
You can use MSXML2 and XPath:
Dim xmldoc As Object
Dim xmlnode As Object
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.setProperty "SelectionLanguage", "XPath"
xmldoc.loadXML response '//The XML Response string
For Each xmlnode In xmldoc.SelectNodes("//*[contains(name(),'LogFileName')]")
Debug.Print xmlnode.Text
Next
For early binding reference Mictosoft XML v3.0 or v6.0.

Get nested element Linq to XML

My XML
<?xml version="1.0" encoding="utf-8"?>
<metadata created="2014-05-15T12:26:07.701Z" xmlns="http://site/cu-2.0#" xmlns:ext="http://site/cu/b-2.0">
<customer-list count="47" offset="0">
<customer id="7123456" type="Cust" ext:mark="1">
<name>Tony Watt</name>
<sort-name>Watt, Tony</sort-name>
<gender>male</gender>
<country>US</country>
<knownAs-list>
<knownAs locale="ko" sort-name="Tony Watt"</knownAs>
<knownAs locale="ja" sort-name="Watt Tony"</knownAs>
</knownAs-list>
<tag-list>
<begin>Country</begin>
<tag count="1">
<name>usa</name>
</tag-list>
</customer>
<customer id="9876543" type="Cust" ext:mark="2">
....
</customer-list>
So i have some code that gets all the data. I went one step further to use Anonymous types and add the values into a class as below
Dim c = From cust As XElement In XDoc.Descendants(ns + "customer")
Select New Customer() With {.Name = cust.Element(ns + "name"),
.Surname = CStr(cust.Element(ns + "surname")),
.Id = cust.Attribute("id"),
.Tag = CStr(cust.Element("tag-list").Element("begin"))}
The above code returns data from the XML, but adding this line of code
.Tag = CStr(cust.Element("tag-list").Element("begin"))
throws an exception, "Object reference not set to an instance of an object". Now theres two possibilities here
I have my code wrong for that particular line (to retrieve 'begin' from the 'tag-list' element)
I know some tag-list elements dont have a nested begin element so that could be adding some confusion. I added Cstr to overcome this but not sure if this is enough?
After reading MSDN it seems using .Descendants (Xdoc.Descendants) would get the all the data from all elements where Elements would return data upto the path i have stated, so as far as i can tell the data 'should' be available with the above code. Could anyone assist me in getting the begin data from tag-list?
The XML namespace declaration is missing. Use
.Tag = CStr(cust.Element(ns + "tag-list").Element(ns + "begin"))

WCF DataContractSerializer Behavior

I'm seeing some unusual behavior when using the DataContractSerializer. I have defined a message contract like so:
namespace MyNamespace.DataContracts
{
[MessageContract(WrapperName = "order", WrapperNamespace = #"http://example.com/v1/order")]
public class MyOrder
{
[MessageBodyMember(Namespace = #"http://example.com/v1/order", Order = 1)]
public MyStore store;
[MessageBodyMember(Namespace = #"http://example.com/v1/order", Order = 2)]
public MyOrderHeader orderHeader;
[MessageBodyMember(Namespace = #"http://example.com/v1/order", Order = 3)]
public List<MyPayment> payments;
[MessageBodyMember(Namespace = #"http://example.com/v1/order", Order = 4)]
public List<MyShipment> shipments;
}
.
.
I'm sending it an XML message that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<order xmlns="http://example.com/v1/order>
<store>
...
</store>
<orderHeader>
...
</orderHeader>
<payments>
<payment>
...
</payment>
</payments>
<shipments>
<shipment>
...
</shipment>
</shipments>
</order>
My service deserializes this XML as expected. Inside my service, I'm using the DataContractSerializer to create an XML string and that's where things get weird. I'm using the serializer like this:
DataContractSerializer serializer = new DataContractSerializer(typeof(MyOrder));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, order);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string outputMessage = sr.ReadToEnd();
}
Once this finishes, the outputMessage contains the following XML:
<?xml version="1.0" encoding="utf-8"?>
<MyOrder xmlns="http://example.com/v1/order" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<order>
<store>
...
</store>
<orderHeader>
...
</orderHeader>
<payments>
<payment>
...
</payment>
</payments>
<shipments>
<shipment>
...
</shipment>
</shipments>
</order>
</MyOrder>
Needless to say, anything expecting to receive the original XML message will fail to parse this. So I guess I have two questions:
Why is the DataContractSerializer
adding the extra outer node to my
XML output?
Is there a way to stop it from doing
this?
Thanks.
I should probably add this is with .NET 4.
You could try using WriteObjectContent instead of WriteObject, but I'm unable to reproduce your problem using the code you supplied. All the extra class defintions that are part of your message contract are empty in my definition, but this is the XML I am getting:
<MyOrder xmlns="http://schemas.datacontract.org/2004/07/SandboxApp"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<orderHeader i:nil="true"/>
<payments i:nil="true"/>
<shipments i:nil="true"/>
<store i:nil="true"/>
</MyOrder>
Which also seems odd, since it seems to ignore the WrapperName. Same result in .NET 3.5 SP1 and .NET 4.0.

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.