locked
Multiple Attribute values into a single XmlnodeList RRS feed

  • Question

  • Hi, friends

                        I had fetched data from CRM using fetchxml..Now I have an xml document which contain attributes and attribute values for the account entity..I want attribute values from the xml document.I will get the 'accountnumber' number values if i use the following

     

    XmlNodeList y = rNodeAccount.SelectNodes("/resultset/result/accountnumber");

    for (int x = 0; x < y.Count; x++)

    {

    string f = y.Item(x).InnerText;

    }

                  I want to retrieve more than one attribute values at the same time..for eg: I want to retrieve accountnumber and name into a nodelist..How can I do this..I can't write like the following,but I actually want this

    XmlNodeList y = rNodeAccount.SelectNodes("/resultset/result/accountnumber,name");

                How can I assign both these attributes to  a single  xmlnodelist
    Wednesday, July 28, 2010 6:17 AM

Answers

  • Hi,

    you can do this like as below

    if(record.SelectSingleNode("./accountnumber")!=null)

    string accountnumber = record.SelectSingleNode("./accountnumber").InnerText;

    else

    string accountnumber =string.Empty;


    Mahain
    • Proposed as answer by HIMBAPModerator Wednesday, July 28, 2010 7:13 AM
    • Marked as answer by Hari nvp Wednesday, July 28, 2010 7:55 AM
    Wednesday, July 28, 2010 7:12 AM
    Moderator

All replies

  • I don't think so you can do this you have to fetch it one by one, like below

    XmlNodeList y = rNodeAccount.SelectNodes("/resultset/result/accountnumber");

    XmlNodeList y = rNodeAccount.SelectNodes("/resultset/result/name");


    Mahain
    Wednesday, July 28, 2010 6:35 AM
    Moderator
  • As for me it is better to use following construction:

    XmlNodeList records = rNodeAccount.SelectNodes("/resultset/result");

    foreach(XmlNode record in records)
    {

    string accountnumber = record.SelectSingleNode("./accountnumber").InnerText;

    string name = record.SelectSingleNode("./name").InnerText;

    }


    Truth is opened the prepared mind

    My blog (english)
    Мой блог (русскоязычный)
    Wednesday, July 28, 2010 6:39 AM
    Moderator
  • Ok, thank you,

                  But, if an attribute is absent in one of the nodes..For eg:,if 'accountnumber' is absent in one of the nodes, "object refernce not set error should arise"..So, I want to set a condition for each of the nodes..if <accountnumber></accountnumber> is not present in a node, I want to set

     string accountnumber =string.Empty;

                      How can I do this? pls help me

    Wednesday, July 28, 2010 7:11 AM
  • Hi,

    you can do this like as below

    if(record.SelectSingleNode("./accountnumber")!=null)

    string accountnumber = record.SelectSingleNode("./accountnumber").InnerText;

    else

    string accountnumber =string.Empty;


    Mahain
    • Proposed as answer by HIMBAPModerator Wednesday, July 28, 2010 7:13 AM
    • Marked as answer by Hari nvp Wednesday, July 28, 2010 7:55 AM
    Wednesday, July 28, 2010 7:12 AM
    Moderator
  • Try to use following code:

    XmlNodeList records = rNodeAccount.SelectNodes("/resultset/result");

    foreach(XmlNode record in records)
    {

    string accountnumber = record.SelectSingleNode("./accountnumber") == null ? "" : record.SelectSingleNode("./accountnumber").InnerText;

    string name = record.SelectSingleNode("./name") == null ? "" : record.SelectSingleNode("./name").InnerText;

    }


    Truth is opened the prepared mind

    My blog (english)
    Мой блог (русскоязычный)
    Wednesday, July 28, 2010 7:47 AM
    Moderator
  • Thnk you very much Mahain,

                      Now my problem is solved..I can proceed with fetchxml

    Wednesday, July 28, 2010 7:55 AM