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.
Related
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:-
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.
How do I work out what the namespace declaration is for the Extension node?
I want to return all of the child nodes under: GPO->User->ExtensionData->Extension
<?xml version="1.0" encoding="utf-16"?>
<GPO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.microsoft.com/GroupPolicy/Settings">
<User>
<VersionDirectory>4</VersionDirectory>
<VersionSysvol>4</VersionSysvol>
<Enabled>true</Enabled>
<ExtensionData>
<Extension xmlns:q1="http://www.microsoft.com/GroupPolicy/Settings/Scripts" xsi:type="q1:Scripts">
<q1:Script>
<q1:Command>Logon.cmd</q1:Command>
<q1:Type>Logon</q1:Type>
<q1:Order>0</q1:Order>
<q1:RunOrder>PSNotConfigured</q1:RunOrder>
</q1:Script>
</Extension>
<Name>Scripts</Name>
</ExtensionData>
</User>
<LinksTo>
<SOMName>an interesting data value</SOMName>
<SOMPath>some data value</SOMPath>
<Enabled>true</Enabled>
<NoOverride>false</NoOverride>
</LinksTo>
</GPO>
This is my attempt:
Dim NS As XNamespace = "http://www.microsoft.com/GroupPolicy/Settings/Scripts"
Dim UserPolCount = XDoc.Descendants(NS + "Extension").First()
I get the following error: Sequence contains no elements
Also, the XML sample I have provided is only a small snippet, the ExtensionData->Extension nodes can be nested in different areas, so I was hoping to find the way of specifying the full path.
Thanks
The Extension Element is still under the root namespace of:
http://www.microsoft.com/GroupPolicy/Settings
Elements under Extension are under the Scripts namespace:
http://www.microsoft.com/GroupPolicy/Settings/Scripts
So you need:
Dim NS As XNamespace = "http://www.microsoft.com/GroupPolicy/Settings/"
Dim NS1 As XNamespace = "http://www.microsoft.com/GroupPolicy/Settings/Scipts"
Dim UserPolCount = XDoc.Descendants(NS + "Extension").First()
Dim ScriptNode = UserPolCount.Elements(NS1 + "Script")
EDIT From the comments:
Dim extension =
XDoc
.Root
.Element(NS + "User")
.Element(NS + "ExtensionData")
.Element(NS + "Extension");
You are using the wrong namespace. You need to use http://www.microsoft.com/GroupPolicy/Settings as the namespace.
The reason is that only the children of Extension are in the Scripts namespace. You can easily see this: The children are all prefixed with q1, the Extension tag itself not. Therefore it is defined in the default namespace, defined by the attribute xmlns="http://www.microsoft.com/GroupPolicy/Settings" on the root tag GPO.
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.
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.