Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

C# XML Nodes

Posted on by 445

Hello Experts,

i have a problem with nodes from a xml file.

I have some <eventitem> nodes and want to loop every item in a variable, but i only get the first one...

<events>Liste</events>
<eventitem>Mitgliederversammlung</eventitem>
<eventitem>Sechzig Jahre</eventitem>

//SEMINARSELEKTION

                                    workshopstring = node["eventitem"].InnerText;
                                    

                                        if (workshopstring.Contains("Tag"))
                                        {

                                            TDMRTeilnahme = true;

                                            TDMRTeilnahme1.Set(executionContext, TDMRTeilnahme);
                                            tracingService.Trace("TDMRTeilnahme zugeordnet");

                                        }
                                        else if (workshopstring.Contains("Masch"))
                                        {

                                            tedx = true;
                                            TedX.Set(executionContext, tedx);
                                            tracingService.Trace("Teilnahme");
                                        }
                                        else
                                        {
                                            QueryExpression workshopquery = new QueryExpression("mrd_seminar");
                                            workshopquery.ColumnSet = new ColumnSet(true);
                                            workshopquery.Criteria.AddCondition("mrd_name", ConditionOperator.Equal, workshopstring);

                                            // query ausführen
                                            EntityCollection retrieveworkshop = service.RetrieveMultiple(workshopquery);

                                            if (retrieveworkshop.Entities.Count == 1)
                                            {
                                                                                              
                                                Entity Workshop1 = retrieveworkshop.Entities[0];
                                                Entity Workshop2 = retrieveworkshop.Entities[0];
                                                Entity Workshop3 = retrieveworkshop.Entities[0];
                                                Entity Workshop4 = retrieveworkshop.Entities[0];

                                                if (Workshop1 != null)
                                                {
                                                    WorkshopA.Set(executionContext, new EntityReference("mrd_seminar", Workshop1.Id));
                                                    tracingService.Trace("Workshop zugeordnet ", Workshop1.ToString());
                                                }
                                                else if (Workshop2 != null)
                                                {
                                                    WorkshopA.Set(executionContext, new EntityReference("mrd_seminar", Workshop2.Id));
                                                    tracingService.Trace("Workshop zugeordnet ", Workshop2.ToString());
                                                }
                                                else if (Workshop3 != null)
                                                {
                                                    WorkshopA.Set(executionContext, new EntityReference("mrd_seminar", Workshop3.Id));
                                                    tracingService.Trace("Workshop zugeordnet ", Workshop3.ToString());
                                                }
                                                else if (Workshop4 != null)
                                                {
                                                    WorkshopA.Set(executionContext, new EntityReference("mrd_seminar", Workshop4.Id));
                                                    tracingService.Trace("Workshop zugeordnet ", Workshop4.ToString());
                                                }

                                            }

                                        }

                                    //Variable clearen
                                    tracingService.Trace(workshopstring);
                                    workshopstring = null;
                                    return;

                                    //ENDE SEMINARVERGABE


Kind Regards
Léon

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: C# XML Nodes

    Hello Leon ,

    Could you please check below article.I hope you can get help from there

    www.csharp-examples.net/xml-nodes-by-name

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: C# XML Nodes

    Hi,

    Not sure why you are doing the following:

    Entity Workshop1 = retrieveworkshop.Entities[0];

    Entity Workshop2 = retrieveworkshop.Entities[0];

    Entity Workshop3 = retrieveworkshop.Entities[0];

    Entity Workshop4 = retrieveworkshop.Entities[0];

    They all return the exact same value, since your are setting this to the first entity:

    All you need is:

    Entity workshop = retrieveworkshop.Entities[0];

    Can you expand on what you are trying to do?

    For querying Xml you need to add a loop or you can use Linq To Xml.

    Check the following doc on how to query descendants:

    docs.microsoft.com/.../how-to-find-descendants-with-a-specific-element-name

    Hope this helps.

  • Suggested answer
    Michel van den Brink Profile Picture
    Michel van den Brink 4,697 on at
    RE: C# XML Nodes

    Hello Léon,

    Which class are you using to read your XML?

     

    1. If you are using System.Xml.Linq.XDocument you can do this:
      IEnumerable<System.Xml.Linq.XElement> elements = doc.Document.Elements("eventitem");
      foreach(System.Xml.Linq.XElement element in elements) {
         // Loop...
      }

       

    2. If you are using System.Xml.XmlDocument, you can do this:
      System.Xml.XmlNodeList nodes = doc.DocumentElement.SelectNodes("//eventitem");
      foreach(System.Xml.XmlNode node in nodes)
      {
         // Loop...
      }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans