Editing XML Output With LINQ



I have an XML output file from a process being run that needs the contents of various fields edited according to a collection of tables in our database. For example, what's included in



<ArrayOfUserReportPreviewListDto xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<UserReportPreviewListDto>
<ExtensionData />
<Id>previewReportFieldsId</Id>
<Field0>-7</Field0>
<Field1>L</Field1>
<Field2>Lab Work Complete</Field2>
<Field3>False</Field3>
<Field4>LabWorkComplete</Field4>
<Field6>False</Field6>
</UserReportPreviewListDto>


would need Field4 changed from LabWorkComplete (tblEnum.FieldTypeDesc) to 2 (tblEnum.FieldTypeNum).


I'm very new to using LINQ, and am not even completely sure it's the best route for this. I've created a DataSet in the project, with a DataTable populated from the database with what I need to work with. And...that's as far as I've got. Right now I'm using a massive list of tedious If statements to accomplish this, and am thinking this avenue may be more efficient than a collection of statements like this.



var xe = XElement.Load("serializer.xml");
string field4Value = xe.XPathSelectElement(@"/UserReportPreviewListDto/Field4").Value;
if (field4Value == "Incomplete")
{
xe.XPathSelectElement(@"/UserReportPreviewListDto/Field4").Value = "0";
}
else if (field4Value == "SendToLab")
{
xe.XPathSelectElement(@"/UserReportPreviewListDto/Field4").Value = "1";
}
else if (field4Value == "LabWorkComplete")
{
xe.XPathSelectElement(@"/UserReportPreviewListDto/Field4").Value = "2";
}


So that's where I am. If LINQ wouldn't be the best avenue, what would be? If it would be, what would be the best way to do it? Additionally, any particularly helpful resources along these lines that can be recommended would be appreciated; I'd much rather learn code than copy code. I'd hate to have to ask this again next week, after all. :)


As always, thanks for the help.


No comments:

Post a Comment