VB.NET Serialization Missing dot right before new line serialization - vb.net

I've been using XML serialization for a while, and today I realized something really odd. If I have a new line right after a "dot" (.), when i deserialize, I lose the dot. Has anyone ever had this happen to them? The following is my serialization code:
Serialize
Dim xmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(SilverWare.Licensing.Common.StoreLicense), New System.Type() {GetType(SilverWare.Licensing.Common.StationLicense)})
Dim gen As LicenseGenerator
If store Is Nothing Then
Throw New ArgumentNullException("store")
ElseIf store.StationLicenses Is Nothing Then
Throw New ArgumentNullException("store.StationLicenses")
ElseIf store.StationLicenses.Length = 0 Then
Throw New ArgumentOutOfRangeException("store.StationLicenses", "Must contain at least one element.")
End If
' Create a license generator for issuing new license keys.
gen = New LicenseGenerator(store)
' Generate store key.
store.LicenseKey = gen.GenerateLicenseKey
' Generate individual station keys.
For Each station In store.StationLicenses
station.LicenseKey = gen.GenerateLicenseKey(station)
Next
' Write license to file.
Using xFile As Xml.XmlWriter = Xml.XmlWriter.Create(licenseFile)
xmlSerializer.Serialize(xFile, store)
xFile.Close()
End Using
Deserialize
Dim xmlDeserializer As New System.Xml.Serialization.XmlSerializer(GetType(SilverWare.Licensing.Common.StoreLicense), New System.Type() {GetType(SilverWare.Licensing.Common.StationLicense)})
Dim result As SilverWare.Licensing.Common.StoreLicense
Using xFile As Xml.XmlReader = Xml.XmlReader.Create(licenseFile)
result = DirectCast(xmlDeserializer.Deserialize(xFile), SilverWare.Licensing.Common.StoreLicense)
xFile.Close()
End Using
Return result
The really funny part is that if I have a space after the dot, or remove the new line character, there are no problems. This only happens if it is dot which I find mind boggling.
Here is a quick sample of my XML file that was created when I serialized:
<?xml version="1.0" encoding="utf-8" ?>
<StoreLicense xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
<ReceiptAddress>98 N. Washington St.
Berkeley Springs West Virginia</ReceiptAddress>
<Name>Ambrae House at Berkeley Springs</Name>
<AliasName>Ambrae House</AliasName>
<Address1>98 N. Washington St.</Address1>
<Address2 />
...
</StoreLicense>
The line that is having the problem is the ReceiptAddress Node.

This post on MSDN seems to answer your question.
MSDN: Serialize String containing only whitespace such as a " " character
From that post, try this:
<XmlAttribute("xml:space")> _
Public SpacePreserve As [String] = "preserve"
This creates a root node like the following:
<DataImportBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:space="preserve">
Jim

Since I was using someone elses dll, I didn't even think that it would be modifying my data when we imported it. What was happening was that the other programmer had a reg_ex that was looking for a dot before a new line. That was my issue, and my grief for 3 months.

Related

Error with SPPID's Interop.Llama library : System.Runtime.InteropServices.COMException (0x80000008): No DBConnect for Data_Dictionary

I'm having the following error usiing SmartPlant P&ID (SPPID) automation library Llama (Interop.Llama.dll):
System.Runtime.InteropServices.COMException (0x80000008): No DBConnect for Data_Dictionary
at Llama._LMEquipments.Collect(LMADataSource& DataSource, _LMAItem& Parent, String& RelationshipName, LMAFilter& Filter)
Here is the code that calls it:
Dim objDS As Llama.LMADataSource
Dim objEquips As Llama.LMEquipments
objDS = New Llama.LMADataSource
objDS.ProjectNumber = Project.SPPIDName & "!" & Project.SPPIDName
objDS.SiteNode = Project.SiteServer
objEquips = New Llama.LMEquipments
objEquips.Collect(objDS) ' throws Exception
Any ideas on what might be wrong?
SPPID is an engineering tool used to develop and manage piping and instrumentation diagrams. Llama is an automation library supplied with SPPID.
This means that your code can't find that specific project in SPPID available plant structures database. Beware that the ProjectNumber string is case sensitive .
In my case, all I had to do was to change project name, from PF2_REF to PF2_Ref (replaced last two letters for lowercase equivalents).

What is the best way to parse GML in VB.Net

I'm looking for the best way to parse GML to return the spatial data. As example here's a GML file:
<?xml version="1.0" encoding="utf-8"?>
<gml:FeatureCollection xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:onecallgml="http://www.pelicancorp.com/onecallgml"
xsi:schemaLocation="http://www.pelicancorp.com/onecallgml http://www.pelicancorp.com/digsafe/onecallgml.xsd">
<gml:featureMember>
<onecallgml:OneCallReferral gml:id="digsite">
<onecallgml:LocationDetails>
<gml:surfaceProperty>
<gml:Polygon srsName="EPSG:2193">
<gml:exterior>
<gml:LinearRing>
<gml:posList>
1563229.00057526 5179234.72234694 1563576.83066077 5179352.36361939 1563694.22647617 5179123.23451613 1563294.42782719 5179000.13697214 1563229.00057526 5179234.72234694
</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceProperty>
</onecallgml:LocationDetails>
</onecallgml:OneCallReferral>
</gml:featureMember>
</gml:FeatureCollection>
How do I iterate through each featureMember, and then its polygon(s) and then get the posList coordinates into an array?
When dealing with XML in VB.NET, I recommend using LINQ to XML. You will probably want to extract more information (e.g. something to tie back to the featureMember), but a simple example could be:
' You will need to import the XML namespace
Imports <xmlns:gml = "http://www.opengis.net/gml">
...
Dim xml As XElement = XElement.Parse(myGmlString) ' or some other method
Dim polys = (
From fm In xml...<gml:featureMember>
From poly In fm...<gml:Polygon>
Select New With {
.Name = poly.#srsName,
.Coords = (poly...<gml:posList>.Value.Trim() _
.Split({" "}, StringSplitOptions.RemoveEmptyEntries) _
.Select(Function(x) CDbl(x))).ToArray()
}
).ToList()
This will give you a List of anonymous types with the polygon name and the coordinates as an array of Double.

query ebay odata with linq

I am pulling information from eBay using their odata feed. I am trying to use linq to pull the specific information we are after. Using linq I can get down to the specific element holding the information we want. What I can't do is figure out how to query the element data to get the specific child elements I want. I could just parse it but really want to learn linq. I am using vb.net as the language. To get the element I am after I use the following:
Sub Main
dim ns = "http://ebayodata.cloudapp.net/"
dim url as string = "http://ebayodata.cloudapp.net/Items?search=1756-L65"
Using reader As XmlReader = XmlReader.Create(url)
While reader.Read()
If reader.NodeType = XmlNodeType.Element AndAlso reader.Name = "entry" Then
GetChildContentElements(reader)
End If
End While
End Using
End Sub
Private Sub GetChildContentElements(reader As XmlReader)
' move to first child
While reader.Read()
If reader.NodeType = XmlNodeType.Element AndAlso reader.Name = "m:properties" Then
Exit While
End If
End While
Dim bookXml As XElement = DirectCast(XNode.ReadFrom(reader), XElement)
Console.WriteLine(bookXml)
End Sub
One of the elements this returns looks like:
<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<d:Id>160917851201</d:Id>
<d:UserId>baltisales</d:UserId>
<d:Title>Allen Bradley 1756-L65 /B ControlLogix Processor 32MB Memory *60 DAYS WARRANTY!*</d:Title>
<d:Subtitle m:null="true"></d:Subtitle>
<d:SellingState>Active</d:SellingState>
<d:TimeLeft>P24DT2H25M33S</d:TimeLeft>
<d:Currency>USD</d:Currency>
<d:CurrentPrice m:type="Edm.Double">6446.14</d:CurrentPrice>
<d:MinimumToBid m:type="Edm.Double">6446.14</d:MinimumToBid>
<d:BidCount m:type="Edm.Int32">0</d:BidCount>
<d:Description m:null="true"></d:Description>
<d:QuantitySold m:type="Edm.Int32">0</d:QuantitySold>
<d:AutoPay m:type="Edm.Boolean">false</d:AutoPay>
<d:CharityId m:null="true"></d:CharityId>
<d:Country>US</d:Country>
<d:Compatibility m:null="true"></d:Compatibility>
<d:GalleryUrl>http://thumbs2.ebaystatic.com/m/m3Y01PfuyFhctnJiEet95Gw/140.jpg</d:GalleryUrl>
<d:GlobalId>EBAY-US</d:GlobalId>
<d:PostalCode>21209</d:PostalCode>
<d:ReturnsAccepted m:type="Edm.Boolean">true</d:ReturnsAccepted>
<d:PrimaryCategoryId>97184</d:PrimaryCategoryId>
<d:SecondaryCategoryId m:null="true"></d:SecondaryCategoryId>
<d:ViewItemUrl>http://www.ebay.com/itm/Allen-Bradley-1756-L65-B-ControlLogix-Processor-32MB-Memory-60-DAYS-WARRANTY-/160917851201?pt=BI_Control_Systems_PLCs</d:ViewItemUrl>
<d:PaymentMethods>PayPal ,VisaMC ,AmEx</d:PaymentMethods>
<d:Condition m:type="eBay.Model.Entities.Condition">
<d:Id m:type="Edm.Int32">3000</d:Id>
<d:Name>Used</d:Name>
</d:Condition>
<d:ListingInfo m:type="eBay.Model.Entities.ListingInfo">
<d:BestOfferEnabled m:type="Edm.Boolean">true</d:BestOfferEnabled>
<d:BuyItNowAvailable m:type="Edm.Boolean">false</d:BuyItNowAvailable>
<d:BuyItNowPrice m:type="Edm.Double" m:null="true"></d:BuyItNowPrice>
<d:ConvertedBuyItNowPrice m:type="Edm.Double" m:null="true"> </d:ConvertedBuyItNowPrice>
<d:Gift m:type="Edm.Boolean">false</d:Gift>
<d:ListingType>StoreInventory</d:ListingType>
<d:StartTime m:type="Edm.DateTime">2012-11-06T23:08:18Z</d:StartTime>
<d:EndTime m:type="Edm.DateTime">2013-04-05T23:13:18Z</d:EndTime>
</d:ListingInfo>
<d:Distance m:type="eBay.Model.Entities.Distance" m:null="true"></d:Distance>
<d:ShippingInformation m:type="eBay.Model.Entities.ShippingInformation">
<d:Delimiter m:null="true"></d:Delimiter>
<d:ExpeditedShipping m:type="Edm.Boolean">true</d:ExpeditedShipping>
<d:HandlingTime m:type="Edm.Int32">1</d:HandlingTime>
<d:OneDayShippingAvailable m:type="Edm.Boolean">true</d:OneDayShippingAvailable>
<d:ShippingServiceCost m:type="Edm.Double">0</d:ShippingServiceCost>
<d:ShippingType>FlatDomesticCalculatedInternational</d:ShippingType>
</d:ShippingInformation>
</m:properties>
I am struggling with querying the above to get the specific child elements we need to work with. The ":" in the tags seems to be confusing using namespaces. What I would like to do is to be able to query the element to get values for d:Id, d:UserId, d:Currentprice, etc. Any suggestions would be welcomed.
Try using WCF data services client. It can generate proxy classes for you that would simplify things. Refer to the documentation here.
Sample code for doing custom url's with generated WCF DS client,
const string baseAddress = "http://ebayodata.cloudapp.net/";
EBayData ebay = new EBayData(new Uri(baseAddress));
var items = ebay.Execute<Item>(new Uri(baseAddress + "Items?search=1756-L65"));
foreach (var item in items)
{
Console.WriteLine(item.Title);
}

parsing tcx document using xdocument selectsinglenode

I am trying to parse an TCX document similiar to the one used in this post: Import TCX into R using XML package
Only I'm trying to use XmlDocument.SelectNodes and SelectSingleNode instead of getNodeSet. The line with xmlns looks like this:
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
If I remove the xmlns and just have , I can parse it without any problems.
My (vb.net) code:
Dim tcxXmlDocument As New System.Xml.XmlDocument()
tcxXmlDocument.Load(tcxFile)
Dim xmlnsManager = New System.Xml.XmlNamespaceManager(tcxXmlDocument.NameTable)
Dim trackpoints As New List(Of Trackpoint)
For Each tpXml As System.Xml.XmlNode In tcxXmlDocument.SelectNodes("//Trackpoint", xmlnsManager)
Dim newTrackpoint As New Trackpoint
With newTrackpoint
.Time = tpXml.SelectSingleNode("Time").InnerText
.LatitudeDegrees = tpXml.SelectSingleNode("Position/LatitudeDegrees").InnerText
.LongitudeDegrees = tpXml.SelectSingleNode("Position/LongitudeDegrees").InnerText
.HeartRateBpm = tpXml.SelectSingleNode("HeartRateBpm").InnerText
End With
trackpoints.Add(newTrackpoint)
Next
How can I configure the XmlNamespaceManager so that I can access the nodes in such a tcx document?
Thanks,
Jason
Use the XmlNamespaceManager.AddNamespace() method to associate a preffix (say "x") with the namespace "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2".
Then in your XPath expression, pefix every element name with the "x" prefix.
Replace this:
//Trackpoint
with:
//x:Trackpoint
Replace this:
Time
with:
x:Time
Replace this:
Position/LatitudeDegrees
with:
x:Position/x:LatitudeDegrees
Replace this:
Position/LongitudeDegrees
with:
x:Position/x:LongitudeDegrees
Finally, replace this:
HeartRateBpm
with:
x:HeartRateBpm

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.