Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

Reading two xml files simultaneously using Linq in C#

I am trying to loop over 2 xml files simultaneously and loading into a list of custom JobModel objects. I am getting NullReferenceException on jobs.ToList() call, obviously my Linq expression is not correct and jobs enumeration is empty.

First xml file

tasks.xml 
<objects>
  <object id="X001">
    <property name="jobDetail" />
    <property name="cronExpressionString" value="0 00 00 ? * MON-FRI" />
  </object>
  <object id="Addf8ae09bf7a47a891114b8246e3108d">
    <property name="jobDetail" ref="TaskExecutor" />
    <property name="cronExpressionString" value="0 00 18 ? * MON,TUE,WED,THU,FRI *" />
  </object>
</objects>

Second xml file

details.xml
<Tasks>
  <Task id="X001">
    <name>task_1</name>
    <desc>desc_1</desc>       
  </Task>  
  <Task id="Addf8ae09bf7a47a891114b8246e3108d">
    <name>task_2</name>
    <desc>desc_2</desc>    
  </Task>
</Tasks>

This is what I have tried in various combinations

XDocument tasks = XDocument.Load(@"tasks.xml");
XDocument details = XDocument.Load(@"details.xml");
var jobs = from t in tasks.Root.Elements()
              join d in details.Root.Elements() on t.Attribute("id").Value equals d.Attribute("id").Value
              select new JobModel
            {
                Id = (string)t.Attribute("id").Value,

                CronExpression = t.Descendants("property")
                   .Where(x => (string)x.Attribute("name") == "cronExpressionString")
                   .Select(x => (string)x.Element("value"))
                   .FirstOrDefault()
            };

jobs.ToList();

Any help is much appreciated. Thank you.

question from:https://stackoverflow.com/questions/65873214/reading-two-xml-files-simultaneously-using-linq-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...