xml parsing - code refactoring issue

前端 未结 2 428
难免孤独
难免孤独 2021-01-29 11:01

I have the following xml:



  
  P1
  

        
2条回答
  •  [愿得一人]
    2021-01-29 11:22

    Given your XML is tiny, you probably don't have to worry too much about performance. You can just do the whole thing in one go, making use of the built in explicit conversions:

    var data = new OutputData
    {
        FirstOne = (string) doc.Descendants("FirstOne").Single(),
        SecondOne = (string) doc.Descendants("SecondOne").Single(),
        IsMale = (bool) doc.Descendants("IsMale").Single(),
        MarkOne = (double) doc.Descendants("MarkOne").Single(),
        MarkTwo = (double) doc.Descendants("MarkTwo").Single(),
        Slope = (double) doc.Descendants("Slope").Single()
    };
    

    As an aside, Descendants will never return anything implementing IList, so you can definitely remove that.

提交回复
热议问题