Add Soap Headers to XDocument - vb.net

I am creating an XDocument with the following structure:
Dim xDocHandle As XDocument =
New XDocument(
New XDeclaration("1.0", Nothing, Nothing),
New XElement("Element",
New XElement("Dialogue",
New XElement("Desc", AppDesc),
New XElement("Num", Num),
New XElement("Ref", Ref),
New XElement("ms", Ms),
New XElement("im", Im))
))
To have the following output:
<Element>
<Dialogue>
<Desc>test</Desc>
<Num>1</Num>
<Ref></Ref>
<ms>2411616</ms>
<im></im>
</Dialogue>
</Element>
I want to add the following headers
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns="">
Should I add them as new XDeclaration , new XElement?
Whats the type of the soap headers?
Any help would be appreciated.

<soap:Envelope> and <soap:Body> are clearly elements. You can do something like this to construct XML with soap header :
'create <Element> node :'
Dim element As XElement = New XElement("Element",
New XElement("Dialogue",
New XElement("Desc", AppDesc),
New XElement("Num", Num),
New XElement("Ref", Ref),
New XElement("ms", Ms),
New XElement("im", Im))
)
'create <soap:Envelope> node and add <Element> as child of <soap:Body> :'
Dim soap As XNamespace = "http://www.w3.org/2001/12/soap-envelope"
Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
New XElement(soap + "Body", element))
'create XDocument and set <soap:Envelope> as content'
Dim xDocHandle As XDocument =
New XDocument(
New XDeclaration("1.0", Nothing, Nothing),
soapEnvelope
)
Output :
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body>
<Element>
<Dialogue>
<Desc>test</Desc>
<Num>1</Num>
<Ref></Ref>
<ms>2411616</ms>
<im></im>
</Dialogue>
</Element>
</soap:Body>
</soap:Envelope>

Related

to read xml using dom cencepts

Iam doing XML reading using dom concepts, But i couldn't retrieve values correctly.
I want to RETRIEVE BELOW PART using for loop statement
<area id="508" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0">
<pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename>
<pagenumber>1</pagenumber>
<left>603</left>
<top>868</top>
<right>764</right>
<bottom>1132</bottom>
<polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon>
</area>
HERE IS THE FULL SAMPLE XML WHERE I WANT TO RETRIEVE USING FOR LOOP.
<?xml version="1.0" encoding="UTF-8"?>
<articles>
<template istemplate="true" twidth="0" theight="0" uwidth="0" uheight="0" ubottom="0"> </template>
<article name="11187_2014_06_14_000002_0004_NFHEZ_00004" id="4">
<articlename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ_00004]]></articlename>
<input_pages>
<page name="11187_2014_06_14_000002_0004_NFHEZ" number="3">
<pagepdfheight>1125</pagepdfheight>
<pagepdfwidth>786</pagepdfwidth>
<pagejpgheight>1125</pagejpgheight>
<pagejpgwidth>786</pagejpgwidth>
<pagejpgresolution>72</pagejpgresolution>
<name><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></name>
<Article_Area>0</Article_Area>
<Article_percentage>0</Article_percentage>
</page>
</input_pages>
<NoOfPage>1</NoOfPage>
<output_pages>
<page number="1" height="0" width="0"/>
</output_pages>
<read>
<area id="508" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0">
<pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename>
<pagenumber>1</pagenumber>
<left>603</left>
<top>868</top>
<right>764</right>
<bottom>1132</bottom>
<polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon>
</area>
<area id="507" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0">
<pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename>
<pagenumber>1</pagenumber>
<left>462</left>
<top>868</top>
<right>601</right>
<bottom>1131</bottom>
<polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon>
</area>
</read>
My code is:
Dim doc As New XmlDocument()
Dim sValue, sPgName As String
Dim sLeft, sTops, sRight, sBottom As String
Dim ObjZoneInfo As CZoneInfo
Try
doc.Load(sFile)
If doc.ChildNodes.Item(0).Name = "xml" Then
If doc.ChildNodes.Item(1).Name = "articles" Then
For i As Integer = 0 To doc.ChildNodes.Item(1).ChildNodes.Count - 1
If doc.ChildNodes.Item(2).ChildNodes.Item(i).Name = "article" Then
ObjZoneInfo = New CZoneInfo
For j As Integer = 0 To doc.ChildNodes.Item(2).ChildNodes.Item(i).ChildNodes.Count - 1
sValue = doc.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(j).InnerText
If doc.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(j).Name = "page_name" Then
If sProFiles.Contains(sValue) = False Then
sProFiles.Add(sValue)
End If
ObjZoneInfo.PageName = sValue : sPgName = sValue
I understand you are searching only for nodes with "area" tag, so try this:
Dim tagAreas as XmlNodeList = doc.GetElementsByTagName("area")
You can then use a For Each loop:
For Each tagArea as XmlElement in tagAreas Then
'______your code here. If you wanted to retrieve the "area" tag I don't understand the code you posted
Next
Now, to access attributes of the area nodes, you must use the following:
Dim area_id as Integer = Integer.parse(tagArea.GetAttribute("id")) 'if you want to obtain id data as integer
Dim area_type as String = tagArea.GetAttribute("type")
'so etc___________________
if you want to access the child nodes:
Dim area_pagenum as Integer = Integer.parse(tagArea("pagenumber"))

XElement Automatically Having xmlns attribute

Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
New XElement(soap + "Body",
New XAttribute("xmlns", "http://www.test.com"),
New XElement("Open",
New XElement("Data",
New XElement("Desc", _dData.Desc),
New XElement("Num", _dData.Num),
New XElement("Ref", _dData.Ref),
New XElement("Mnn", _dData.Mnn),
New XElement("Ftp", _dData.Ftp))
)))
The following is giving this output:
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns="http://www.test.com">
<Open xmlns="">
<Data>
<Desc>testApp</Desc>
<Num>1</Num>
<Ref></Ref>
<Mnn>116</Mnn>
<Ftp></Ftp>
</Data>
</Open>
</soap:Body>
</soap:Envelope>
The question is why did the <Open> XElement automatically get an xmlns="" attribute?
I want the same output but without any attribute to the XElement <Open>
Any help would be appreciated.
That's because the XML has default namespace (xmlns="...") declared at <Open> element. In XML, all descendant elements inherits default namespace automatically from the ancestor, unless explicitly set otherwise (f.e by using prefix that points to different namespace, or by declaring different default namespace at the descendant level).
Using the code you tried, descendants of <Body> are set in no namespace. You need to set descendants of <Open> element to be in the same default namespace by using XNamespace, for example :
XNamespace ns = "http://www.test.com"
Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
New XElement(soap + "Body",
New XAttribute("xmlns", "http://www.test.com"),
New XElement(ns+"Open",
New XElement(ns+"Data",
New XElement(ns+"Desc", _dData.Desc),
New XElement(ns+"Num", _dData.Num),
New XElement(ns+"Ref", _dData.Ref),
New XElement(ns+"Mnn", _dData.Mnn),
New XElement(ns+"Ftp", _dData.Ftp))
)))
You need to create each element in its namespace:
XNamespace t = "http://www.test.com";
New XElement(t + "Open",
New XElement(t + "Data",
New XElement(t + "Desc", _dData.Desc),
New XElement(t + "Num", _dData.Num),
New XElement(t + "Ref", _dData.Ref),
New XElement(t + "Mnn", _dData.Mnn),
New XElement(t + "Ftp", _dData.Ftp))

Adding namespace limits results Linq to XML

Ive read a few articles on Linq to XML and either ive picked it up wrong or missing some piece of the puzzle.
What im trying to achieve is to load some XML, get required data by different named fields and nodes/elements. Here is the 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>
</customer>
<tag-list>
<tag count="1">
<name>Country</name>
</tag>
<tag count="1">
<name>usa</name>
</tag>
<customer id="9876543" type="Cust" ext:mark="2">
So i can load the XML and i can display data. Heres a snippet of the code
Dim ns As XNamespace = "http://site/cu-2.0#"
Dim XDoc As XDocument = XDocument.Parse(SomeXML)
For Each c As XElement In XDoc.Descendants(ns + "name")
Response.Write(c)
Next
So this displays all the elements with "name". The problem i have here is i want the customer name but not the tag-list country name (see last few lines of the XML)
Ideally i want to return all the details for each customer but adding the namespace limits the me to all the elements with name when i want other data too. If i remove the namespace i get no results returned so im unsure what to do next?
Ive read a ton of articles but i cant seem to work out what needs to be done or if ive gone down the wrong path? Please remember i have tried other methods which i can post if anyone likes but after reading MSDN and other articles i think ive confused myself or missed out a step.
I think you simply want to use
Dim ns As XNamespace = "http://site/cu-2.0#"
Dim XDoc As XDocument = XDocument.Parse(SomeXML)
For Each c As XElement In XDoc.Descendants(ns + "customer")
Response.Write(c.Element(ns + "name").Value)
Next
If you are trying to get name from customer give this a try
Dim xe As XElement =
<customer-list count="47" offset="0">
<customer id="7123456" type="Cust" 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>
</customer>
<customer id="1" type="Cust" mark="1">
<name>Fred Flintstone</name>
<sort-name>Flintstone, Fred</sort-name>
<gender>male</gender>
<country>US</country>
<knownAs-list>
<knownAs locale="ko" sort-name="Fred Flintstone"></knownAs>
<knownAs locale="ja" sort-name="Flintstone Fred"></knownAs>
</knownAs-list>
</customer>
<tag-list>
<tag count="1">
<name>Country</name>
</tag>
<tag count="1">
<name>usa</name>
</tag>
</tag-list>
</customer-list>
Dim ie As IEnumerable(Of XElement) = From c As XElement In xe.Elements
Where c.Name.LocalName = "customer"
Select c From n As XElement In c.Elements
Where n.Name.LocalName = "name" Select n
For Each r As XElement In ie
Debug.WriteLine(r.Value)
Next

webservice with multiple parameter

I create a webservice with axis2 like that:
public void setWeather(#WebParam(name = "weather") Weather weather,
#WebParam(name = "city") String city,
#WebParam(name = "country") String country) {
System.out.println("weather:" + weather);
System.out.println("city:" + city);
System.out.println("country:" + country);
this.weather = weather;
}
Then I send a soap message:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns2:setWeather xmlns:ns2="http://service.pojo.sample">
<ns2:weather>
<ns1:forecast xmlns:ns1="http://data.pojo.sample/xsd">rain</ns1:forecast>
<ns1:howMuchRain xmlns:ns1="http://data.pojo.sample/xsd">102.3</ns1:howMuchRain>
<ns1:rain xmlns:ns1="http://data.pojo.sample/xsd">true</ns1:rain>
<ns1:temperature xmlns:ns1="http://data.pojo.sample/xsd">20.3</ns1:temperature>
</ns2:weather>
<ns2:country>china</ns2:country>
</ns2:setWeather>
</soapenv:Body>
</soapenv:Envelope>
And I receive
weather:Weather [temperature=20.3, forecast=rain, rain=true, howMuchRain=102.3]
city:china
country:null
ns2:country node have been assigned null, why this happens?

Linq To Xml - Unexpected search results

Just when I was thinking that I had Linq To Xml sussed I'm faced with yet another error! I think if I was to understand the linq search process in general better I might have more success, so any good links regarding that are also welcome. To my problem however; using the code below:
Dim xd As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<root>
<element>
<subelement id="1"/>
<subelement id="2"/>
<subelement id="3"/>
</element>
<element>
<subelement id="4"/>
<subelement id="1"/>
<subelement id="5"/>
</element>
</root>
Dim results = _
From q In xd.Descendants.<element> _
Where q.<subelement>.#id = 1
For Each xe As XElement In results
Console.WriteLine(xe.ToString)
Next
I would have expected the above code to return both 'element' nodes, but it only returns the first because it only searches the first 'subelement' node within 'element', how can I make the where clause apply to all 'subelement' nodes?
You could do
Dim results = _
From q In xd.Descendants.<element> _
From p In q.<subelement> _
Where p.#id = 1
(That is, if my VB.NET is up to the task here... I could do it in C#. Please feel free to edit.)