C# LinQ XDocument - How I could optimize the analysis?

Markus Freitag 3,786 Reputation points
2022-02-28T17:06:54.407+00:00

Hello,

I need to read an XML file under certain criteria.
I have a hard time with this from time to time.
The code works, but I think it could be better.

How could I optimize the analysis?

How does the experts do it? How do you guys go about it?
Could I load the result into a new structure? Select or new Select?

Maybe only type =="2D"
Maybe only type =="2D" and type="TEXT"

Thanks in advance for tips.

<ROOT info="00013|09|00">
  <POSITION panel_time="12000000" group="1" number="2"  quality="AC" />
  <CUSTOM>
    <PARAMETERS>
      <DRAWOBJECTS>
        <DRAWOBJECT type="2D">
          <CODETYPE v="8" />
          <POWER v="74" />
          <MARKSPEED v="178" />
        </DRAWOBJECT>
        <DRAWOBJECT type="1D">
          <CODETYPE v="8" />
          <POWER v="94" />
          <MARKSPEED v="778" />
        </DRAWOBJECT>
        <DRAWOBJECT type="TEXT">
          <CODETYPE v="2" />
          <POWER v="44" />
          <MARKSPEED v="678" />
        </DRAWOBJECT>
     </PARAMETERS>
    </CUSTOM>
</ROOT>    


var elementsPosEnd = xdoc.Root.Descendants("DRAWOBJECTS").Elements();
foreach (var item in elementsPosEnd)
{
    string r = item.Attribute("type").Value;
    if (r.ToLower() == "datamatrix")
    {
        string value = item.Element("POWER").Attribute("v").Value;
        value = item.Element("MARKSPEED").Attribute("v").Value;
    }
}

var lstDraw = xdoc.Root?.Element("CUSTOM").Element("PARAMETERS").Elements("DRAWOBJECTS").ToList().Where(x=>x.Element("DRAWOBJECT").Attribute("type").Value.ToLower() == "datamatrix").FirstOrDefault();

string speed = lstDraw.Element("DRAWOBJECT").Element("MARKSPEED").Attribute("v").Value;
string POWER = lstDraw.Element("DRAWOBJECT").Element("POWER").Attribute("v").Value;
string frequence = lstDraw.Element("DRAWOBJECT").Element("T1").Attribute("v").Value;    


var lstDraw2 = xdoc.Root?.Element("CUSTOM").Element("PARAMETERS").Elements("DRAWOBJECTS").ToList().Where(x => x.Element("DRAWOBJECT").Attribute("type").Value.ToLower() == "2d" || x.Element("DRAWOBJECT").Attribute("type").Value.ToLower() == "text").ToList();

    if (lstDraw2 != null)
    {
        var result = from s in lstDraw2
                     select new
                     {
                         Speed2 = s.Element("DRAWOBJECT").Element("MARKSPEED").Attribute("v").Value,
                         Power2 = s.Element("DRAWOBJECT").Element("POWER").Attribute("v").Value
                     };                
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,248 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,325 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,409 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,046 Reputation points Microsoft Vendor
    2022-03-01T06:20:49.737+00:00

    Hi @Markus Freitag ,

    Your query looks fine to me, except the duplicate Element() method. You can try to use the XContainer.Descendants Method, which returns a collection of the descendant elements for this document or element, in document order.

    By using the Descendants method, the query looks like this:

            var lastDraw = xdoc.Root?.Descendants("DRAWOBJECT").Where(c => c.Attribute("type").Value.ToLower() == "text").FirstOrDefault();  
    
            string speed = lastDraw.Element("MARKSPEED").Attribute("v").Value;  
            string POWER = lastDraw.Element("POWER").Attribute("v").Value;  
            string frequence = lastDraw.Element("CODETYPE").Attribute("v").Value;  
    
            var lastDraw2 = xdoc.Root?.Descendants("DRAWOBJECT").Where(c => c.Attribute("type").Value.ToLower() == "2d" || c.Attribute("type").Value.ToLower() == "text").ToList();  
            if (lastDraw2 != null)  
            {  
                var result = from s in lastDraw2  
                             select new  
                             {  
                                 Speed2 = s.Element("MARKSPEED").Attribute("v").Value,  
                                 Power2 = s.Element("POWER").Attribute("v").Value  
                             };  
            }  
    

    The result is like this:

    178771-1.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful