When you pass a string to methods like Descendants() or Elements(), the code is implicitly converted to an XName with no namespace.
Fix is to add a namespace to your method calls, something like this:
XNamespace namespace = "http://www.hotelbeds.com/schemas/2005/06/messages";
var elements = from x in yourXmlElements.Descendants(namespace + "AuditData")
select new
{
// Select items here
};
Or you could use the LocalName property:
var elements = from x in yourXmlElements.Descendants()
where x.Name.LocalName = "AudtData"
select new
{
// Select items here
};
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
I come here to check whether the problem is solved or not……If yes, please mark answer or otherwises you can just feedback.
PS: A tag with namespace isn't as what we can see it's normal. So you should use XNameSpace to import the namespace so as to let the xml file "recognize" it.
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim a As New HotelValuedAvailRequest
Dim xml = a.HotelValuedAvailRQ("20121225", "20121227", "BCN", 1, 2, 0, HttpContext.Current.Session.SessionID) 'This function return string with xml markup
Dim xd As XDocument = XDocument.Parse(xml, LoadOptions.None)
Dim ns1 As String = "http://schemas.xmlsoap.org/soap/envelope/"
Dim q = From d In xd.Descendants Where d.Name.LocalName.Contains(ns1) Select d.Elements().<ServiceHotel>.<HotelInfo>.<Name>
For Each x In q
Response.Write(x.ToString)
Next
'Dim b As New HotelValuedAvailRS
'b.AvailToken = q
'Response.Write(q)
End Sub
Imports System.Xml.Linq
ModuleModule1Sub Main()
Dim xd AsXDocument = XDocument.Load("XMLFile1.xml")
Dim xm = XNamespace.Get("http://www.hotelbeds.com/schemas/2005/06/messages")
Dim result = From item In xd.Descendants(xm + "AuditData").Elements()
SelectNewWith
{.ElementName = item.Name.LocalName,
.ElementValue = item.Value
}
ForEach item In result
Console.WriteLine(item.ElementName &"<===>"& item.ElementValue)
NextEndSubEndModule
Mamboking
Member
183 Points
125 Posts
LINQ to XML. Troubles with namespace.
Dec 05, 2012 11:42 AM|LINK
Here is strange things. When I'm using LINQ to XML where is nodes do not have name space, everything is working perfectly.
Like this one.
But when there is something like that
I can't get any node's value. Code always return null.
Why this happen? How can I remove all namespace values of specific type (xmlns, xsi)? Is there other way?
DarrellNorto...
All-Star
86773 Points
9643 Posts
Moderator
MVP
Re: LINQ to XML. Troubles with namespace.
Dec 05, 2012 05:36 PM|LINK
When you pass a string to methods like Descendants() or Elements(), the code is implicitly converted to an XName with no namespace.
Fix is to add a namespace to your method calls, something like this:
XNamespace namespace = "http://www.hotelbeds.com/schemas/2005/06/messages"; var elements = from x in yourXmlElements.Descendants(namespace + "AuditData") select new { // Select items here };Or you could use the LocalName property:
var elements = from x in yourXmlElements.Descendants() where x.Name.LocalName = "AudtData" select new { // Select items here };Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ to XML. Troubles with namespace.
Dec 06, 2012 01:38 AM|LINK
Hi,
I come here to check whether the problem is solved or not……If yes, please mark answer or otherwises you can just feedback.
PS: A tag with namespace isn't as what we can see it's normal. So you should use XNameSpace to import the namespace so as to let the xml file "recognize" it.
Mamboking
Member
183 Points
125 Posts
Re: LINQ to XML. Troubles with namespace.
Dec 06, 2012 02:37 AM|LINK
Thanks Darell,
let me try.
Mamboking
Member
183 Points
125 Posts
Re: LINQ to XML. Troubles with namespace.
Dec 06, 2012 12:04 PM|LINK
Wel, it's not working. Any other ideas?
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click Dim a As New HotelValuedAvailRequest Dim xml = a.HotelValuedAvailRQ("20121225", "20121227", "BCN", 1, 2, 0, HttpContext.Current.Session.SessionID) 'This function return string with xml markup Dim xd As XDocument = XDocument.Parse(xml, LoadOptions.None) Dim ns1 As String = "http://schemas.xmlsoap.org/soap/envelope/" Dim q = From d In xd.Descendants Where d.Name.LocalName.Contains(ns1) Select d.Elements().<ServiceHotel>.<HotelInfo>.<Name> For Each x In q Response.Write(x.ToString) Next 'Dim b As New HotelValuedAvailRS 'b.AvailToken = q 'Response.Write(q) End SubDecker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ to XML. Troubles with namespace.
Dec 07, 2012 12:22 AM|LINK
Use XNamespace.Get:
Mamboking
Member
183 Points
125 Posts
Re: LINQ to XML. Troubles with namespace.
Dec 07, 2012 02:05 AM|LINK
Thanks Decker, let me try this solution.
Mamboking
Member
183 Points
125 Posts
Re: LINQ to XML. Troubles with namespace.
Dec 07, 2012 06:35 AM|LINK
Decker! Thank s a lot!!!! It's working!