简体   繁体   中英

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

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUserReportPreviewListDto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <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>
  <UserReportPreviewListDto>
    <ExtensionData />
    <Id>previewReportFieldsId</Id>
    <Field0>-6</Field0>
    <Field1>S</Field1>
    <Field2>Sent to Lab</Field2>
    <Field3>False</Field3>
    <Field4>SentToLab</Field4>
    <Field6>False</Field6>
  </UserReportPreviewListDto>
  <UserReportPreviewListDto>
    <ExtensionData />
    <Id>previewReportFieldsId</Id>
    <Field0>-5</Field0>
    <Field1>V</Field1>
    <Field2>Void</Field2>
    <Field3>False</Field3>
    <Field4>Void</Field4>
    <Field6>True</Field6>
    <Field7>12/11/2013</Field7>
    <Field9>769</Field9>
  </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.

Your XML structure is weird. Field0...Field6 is not common, there are usually meaningful names in there. You can always write a function that encapsulates string to integer string conversion, and just provide an xpath as an argument. Then go higher level, provide xpath + conversion delegate, and from this point it's as easy as one line per property. Here is an implementation example:

using System;
using System.Xml.Linq;
using System.Xml.XPath;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      var xe = XElement.Load("serializer.xml");
      ConvertValue(xe, @"/UserReportPreviewListDto/Field4", TranslateValueField4);
    }

    private static void ConvertValue(XElement xe, string xpath, TranslateValue translator)
    {
      string field4Value = xe.XPathSelectElement(xpath).Value;
      xe.XPathSelectElement(xpath).Value = translator(field4Value);
    }

    private delegate string TranslateValue(string value);

    private static string TranslateValueField4(string value)
    {
      switch (value)
      {
        case "Incomplete" :
          return "0";
        case "SendToLab" :
          return "1";
        case "LabWorkComplete":
          return "2";
        default:
          throw new NotImplementedException(); //or provide handling for unknown values
      }
    }
  }
}

You can also avoid using xpath, and just iterate using foreach:

static void Main(string[] args)
{
  var doc = XDocument.Load(@"input.xml");
  foreach (var xe in doc.Root.Elements("UserReportPreviewListDto"))
  {
    ConvertValue(xe, "Field4", TranslateValueField4);
  }
  //doc.Save(path);
}

private static void ConvertValue(XElement xe, string fieldName, TranslateValue translator)
{
  //.Element returns Nothing if element is missing, may want to handle this case
  XElement field4 = xe.Element(fieldName);
  string field4Converted = TranslateValueField4(field4.Value);
  field4.SetValue(field4Converted);
}

I always, always, always prefect to store xml into custom classes and then work with them inside my C# environment. Makes the process of modifying it feel much more natural. Please look at my question here to see the best way to do this. It takes a bit more time, but it makes things SO much easier in the long run. You said you wanted the best route and to learn, right? ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM