NAV dotnet and XDocument.Descendants

thegunzo
Member Posts: 274
Hi
does anyone have a solution for the XDocument.Decendants function in NAV ?
in C#
that will return identity as XElement
Don't know how to do the .FirstOrDefault
Here is the code I have tried
does anyone have a solution for the XDocument.Decendants function in NAV ?
in C#
var identity = xmlDoc.Descendants(XName.Get("Identity", "http://schemas.xmlsoap.org/ws/2006/02/addressingidentity")).FirstOrDefault();
that will return identity as XElement
Don't know how to do the .FirstOrDefault
Here is the code I have tried
OBJECT Codeunit 50437 Certificate Management { OBJECT-PROPERTIES { Date=11.07.13; Time=11:06:45; Modified=Yes; Version List=BL7.00; } PROPERTIES { OnRun=BEGIN MESSAGE(GetServerCertificateValue('https://ws.b2b.is/Statements/20130201/BillService.svc')); END; } CODE { LOCAL PROCEDURE GetWsdlContent@1000000007(wsdlUrl@1000000000 : Text) wsdlContent : Text; VAR WebClient@1000000001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebClient"; BEGIN WebClient := WebClient.WebClient; wsdlContent := WebClient.DownloadString(wsdlUrl); END; LOCAL PROCEDURE GetServerCertificateValue@1000000021(ServiceUrl@1000000000 : Text) encodedValue : Text; VAR xmlDoc@1000000001 : DotNet "'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.Linq.XDocument"; xName@1000000004 : DotNet "'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.Linq.XName"; Enum@1000000010 : DotNet "'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Linq.Enumerable"; Identities@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.IEnumerable`1"; Identity@1000000005 : DotNet "'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.Linq.XElement"; keyInfo@1000000007 : DotNet "'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.Linq.XElement"; x509Data@1000000008 : DotNet "'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.Linq.XElement"; x509Certificate@1000000009 : DotNet "'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.Linq.XElement"; wsdlUrl@1000000006 : Text; wsdlContent@1000000002 : Text; BEGIN wsdlUrl := ServiceUrl + '?wsdl'; wsdlContent := GetWsdlContent(wsdlUrl); xmlDoc := xmlDoc.Parse(wsdlContent); Identities := xmlDoc.Descendants(xName.Get('Identity','http://schemas.xmlsoap.org/ws/2006/02/addressingidentity')); Enum := Identities.GetEnumerator; //Identity := Enum.FirstOrDefault(Identities); IF ISNULL(Identity) THEN ERROR(STRSUBSTNO('Unable to Parse Identity from Wsdl Content, Url:{%1}',wsdlUrl)); keyInfo := Identity.Element(xName.Get('KeyInfo', 'http://www.w3.org/2000/09/xmldsig#')); IF ISNULL(keyInfo) THEN ERROR(STRSUBSTNO('Unable to Parse keyInfo from Wsdl Content, Url:{%1}', wsdlUrl)); x509Data := keyInfo.Element(xName.Get('X509Data', 'http://www.w3.org/2000/09/xmldsig#')); IF ISNULL(x509Data) THEN ERROR(STRSUBSTNO('Unable to Parse keyInfo/X509Data from Wsdl Content, Url:{%1}', wsdlUrl)); x509Certificate := x509Data.Element(xName.Get('X509Certificate', 'http://www.w3.org/2000/09/xmldsig#')); IF ISNULL(x509Certificate) THEN ERROR(STRSUBSTNO('Unable to Parse keyInfo/X509Data/x509Certificate from Wsdl Content, Url:{%1}', wsdlUrl)); encodedValue := x509Certificate.Value; END; BEGIN { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Xml.Linq; using System.Security.Cryptography.X509Certificates; using System.Net; /// <summary> /// Gets the content of the WSDL as string. /// </summary> /// <param name="wsdlUrl">The WSDL URL.</param> /// <returns></returns> private static string GetWsdlContent(string wsdlUrl) { try { using (WebClient client = new WebClient()) { var wsdlContent = client.DownloadString(wsdlUrl); return wsdlContent; } } catch (Exception ex) { throw new Exception(string.Format("Unable to Download Wsdl, Url:{0}, {1}", wsdlUrl, ex.Message), ex); } } public static string GetServerCertificate(string address) { var wsdlUrl = string.Concat(address, "?wsdl"); var wsdlContent = GetWsdlContent(wsdlUrl); try { var xmlDoc = XDocument.Parse(wsdlContent); var identity = xmlDoc.Descendants(XName.Get("Identity", "http://schemas.xmlsoap.org/ws/2006/02/addressingidentity")).FirstOrDefault(); if (identity == null) { throw new Exception(string.Format("Unable to Parse Identity from Wsdl Content, Url:{0}", wsdlUrl)); } var keyInfo = identity.Element(XName.Get("KeyInfo", "http://www.w3.org/2000/09/xmldsig#")); if (keyInfo == null) { throw new Exception(string.Format("Unable to Parse keyInfo from Wsdl Content, Url:{0}", wsdlUrl)); } var x509Data = keyInfo.Element(XName.Get("X509Data", "http://www.w3.org/2000/09/xmldsig#")); if (x509Data == null) { throw new Exception(string.Format("Unable to Parse keyInfo/X509Data from Wsdl Content, Url:{0}", wsdlUrl)); } var x509Certificate = x509Data.Element(XName.Get("X509Certificate", "http://www.w3.org/2000/09/xmldsig#")); if (x509Certificate == null) { throw new Exception(string.Format("Unable to Parse keyInfo/X509Data/x509Certificate from Wsdl Content, Url:{0}", wsdlUrl)); } var encodedValue = x509Certificate.Value; } catch (Exception ex) { throw new Exception(string.Format("Unable to Parse Wsdl Content, Url:{0}, {1}", wsdlUrl, ex.Message), ex); } return encodedValue; } } END. } }
________________________________
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com
0
Comments
-
Could you first show a short example of the data in de xml, and what values you are trying to get0
-
The source wsdl url is in the code example________________________________
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com0 -
Hello,
NAV doesn't support linq extensions [-X
You might need to find another approach doing this. Or you can try to find the way using enumerator and casting object. Haven't tried myself and seems might be also dead end.
Igor Pchelnikov0 -
Thank you Igor for clearing this up.
A custom .dll is the solution then...________________________________
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com0 -
Uhm.. guys? This works just fine: (NAV 2015)
Name DataType Subtype Length
XDocument DotNet System.Xml.Linq.XDocument.'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
XName DotNet System.Xml.Linq.XName.'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
IEnumerable DotNet System.Collections.IEnumerable.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
IEnumerator DotNet System.Collections.IEnumerator.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
XElement DotNet System.Xml.Linq.XElement.'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
XAttribute DotNet System.Xml.Linq.XAttribute.'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
LOCAL ParseXmlValue(XmlContent : Text) Value : Text
XDocument := XDocument.XDocument();
XDocument := XDocument.Parse(XmlContent);
IEnumerable := XDocument.Descendants(XName.Get('Data', Namespace));
IEnumerator := IEnumerable.GetEnumerator();
IEnumerator.MoveNext();
XElement := IEnumerator.Current();
XAttribute := XElement.Attribute(XName.Get('Type', Namespace));
exit(XElement.Value +' ; '+ XAttribute.Value);0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions