locked
How to extract data from below mentioned xml RRS feed

  • Question

  • Hi I am getting below mentioned xml from biztalk processing.I want to extract firstname and lastname value from this Xml i.e-(Vijay malik)

    <?xml version="1.0" encoding="utf-8" ?>
    - <ns0:Response xmlns:ns0="http://schemas.microsoft.com/crm/2007/BizTalkAdapter/isb/Response">
    - <Header>
      <ReturnCode>1</ReturnCode>
      <ErrorCode />
      <ErrorString />
      <Retryable />
      </Header>
    - <Body>
      <Message><prefix:ExecuteFetchResponse xmlns:prefix="http://192.168.1.243/schemas.microsoft.com/crm/2007/isb/ExecuteFetchResponse"><FetchXmlResult>&lt;resultset morerecords="0" paging-cookie="&amp;lt;cookie page=&amp;quot;1&amp;quot;&amp;gt;&amp;lt;contactid last=&amp;quot;{407367FF-3646-DF11-B704-0003FF75BAF3}&amp;quot; first=&amp;quot;{407367FF-3646-DF11-B704-0003FF75BAF3}&amp;quot; /&amp;gt;&amp;lt;/cookie&amp;gt;"&gt;&lt;result&gt;&lt;firstname&gt;Vijay&lt;/firstname&gt;&lt;lastname&gt;Malik&lt;/lastname&gt;&lt;contactid&gt;{407367FF-3646-DF11-B704-0003FF75BAF3}&lt;/contactid&gt;&lt;/result&gt;&lt;/resultset&gt;</FetchXmlResult></prefix:ExecuteFetchResponse></Message>
      </Body>
      </ns0:Response>

    please provice the method for the same.

    Thanks
    Manoj
    Tuesday, September 28, 2010 4:47 AM

Answers

  • I coded up a sample how to get to the data you need.

    System.Xml.

    XmlDocument wrappedXmlDocument = new System.Xml.XmlDocument();
    wrappedXmlDocument.Load(
    @"D:\Temp\work1\SampleInput.xml");
    XmlNode unWrapNode = wrappedXmlDocument.SelectSingleNode(@"//FetchXmlResult");
    // Unwrap first node
    string unWrappedXML = unWrapNode.FirstChild.Value;
    // Now load that up as a new doc
    System.Xml.XmlDocument xDoc2 = new System.Xml.XmlDocument();
    xDoc2.LoadXml(unWrappedXML);
    // Now fetch the date we really want
    XmlNode firstName = xDoc2.SelectSingleNode(@"//resultset/result/firstname");
    XmlNode lastName = xDoc2.SelectSingleNode(@"//resultset/result/lastname");
    // Write them out
    System.Diagnostics.Trace.WriteLine(firstName.FirstChild.Value);
    System.Diagnostics.
    Trace.WriteLine(lastName.FirstChild.Value);


    Well0549, Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread
    Tuesday, September 28, 2010 7:14 AM

All replies

  • Hi Manoj,

    If you want to parse this XML message inside BizTalk orchestration then you can use XPath function in Message Assignment shape.

    Have a look into this http://geekswithblogs.net/sthomas/archive/2004/10/25/13269.aspx

    And from C# class, You can use the XPathNavigator  class.

    http://www.codeproject.com/KB/cpp/myXPath.aspx

    http://support.microsoft.com/kb/308333/en-us

    Regards,

    Abhijit


    Abhijit Mahato - MCTS BizTalk Server blog: http://abhijitmahato.wordpress.com/ Please "Mark as Answer" if Post has Answered the Question
    Tuesday, September 28, 2010 5:16 AM
  • Hi Manoj,

    Easiest way to get the xpath would be,
    1. Import the XML as XSD using wizard
    2. Slect the element and copy the xpath from the properties window.

     


    Kishor Kumar M
    Tuesday, September 28, 2010 6:15 AM
  • Well you have a message in a message here.

    So first you have to import the XSD of the first document. I beleive this is (<ns0:Response xmlns:ns0="http://schemas.microsoft.com/crm/2007/BizTalkAdapter/isb/Response">)

    Then when you receive this message, you will see that the content that you want is encoded.... so we have to get to that and decode it.

    You simply could simply make a distinguished field of FetchXmlResult so it's really easy to get that content. But then you have a encoded content. This is not what you want, you probably want to have the decoded content.

    • For that, load up the entire message in a xmldocuemnt.
    • Then use the xpathnavigator to move to the desired node
    • get the value of that node. (NOT THE TEXT ! cause thats encoded)
    • If you get the value the .net framework will automatically decode for you.
    • You will then have a true xmldocument that represents the data you are after.
    • You can navigate that again with the xpathnavigator

    But now it gets tricky since you have encoded xml stored inside


    Well0549, Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread
    Tuesday, September 28, 2010 6:42 AM
  • I coded up a sample how to get to the data you need.

    System.Xml.

    XmlDocument wrappedXmlDocument = new System.Xml.XmlDocument();
    wrappedXmlDocument.Load(
    @"D:\Temp\work1\SampleInput.xml");
    XmlNode unWrapNode = wrappedXmlDocument.SelectSingleNode(@"//FetchXmlResult");
    // Unwrap first node
    string unWrappedXML = unWrapNode.FirstChild.Value;
    // Now load that up as a new doc
    System.Xml.XmlDocument xDoc2 = new System.Xml.XmlDocument();
    xDoc2.LoadXml(unWrappedXML);
    // Now fetch the date we really want
    XmlNode firstName = xDoc2.SelectSingleNode(@"//resultset/result/firstname");
    XmlNode lastName = xDoc2.SelectSingleNode(@"//resultset/result/lastname");
    // Write them out
    System.Diagnostics.Trace.WriteLine(firstName.FirstChild.Value);
    System.Diagnostics.
    Trace.WriteLine(lastName.FirstChild.Value);


    Well0549, Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread
    Tuesday, September 28, 2010 7:14 AM
  • Hi Well0549,

    Thanks very much for your code and explanation. I have the same issue to sort it out. I am new in Biztalk. Can you please give some more information about how to use this code within an orchestration.

    Kind Regards

    Monday, October 18, 2010 10:04 PM
    1. Create a seperate c# helper class project
    2. make sure your class is public and serializable.
    3. Create a helper method that does stuff for you.... (You can have xmldocument as in parameter)
    4. Make it return whatever you want.

    then.....

    1. In the BizTalk project reference the helper classes.
    2. Declare an orchestration variable of type YourHelperClass
    3. Insert an expression shape.
    4. In the experssion shape, Invoke the method. ( it will take a message as input parameter, inside the helperclass it will be an xmldocument)
    5. do whatever yoy want with the results of your ghelper

    have fun !

     


    Well0549, Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread
    Tuesday, October 19, 2010 8:40 AM