本文介绍了XML解析 - 代码重构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:

I have the following xml:

<?xml version="1.0" encoding="utf-8"?>
<RootData>
  <PassResult>
  <FirstOne>P1</FirstOne>
  <SecondOne>P2</SecondOne>
 <IsMale>false</IsMale>
</PassResult>
<TestResult>
  <MarkOne>100</MarkOne>
  <MarkTwo>50</MarkTwo>
  <Slope>30</Slope>
</TestResult>
<ToneTestResult>
  <TotalTime>2</TotalTime>
  <CorrectPercentage>90</CorrectPercentage>
</ToneTestResult>
<QuestionnaireResult Version="1">
  <Question Id="50">
   <Answer Id="B" />
 </Question>
  <Question Id="60">
   <Answer Id="A" />
 </Question>
 </QuestionnaireResult>
</RootData>



我有下面的代码是不是都在寻找一个好。很多repeatative链接查询。
我如何重构以更结构化的方式验证码填写OutputData对象。我现在不想将它更改为XmlSerializer的: - (

I have the following code which is not at all looking a good one. Lots of repeatative link queries.How can I refactor this code in a more structured way to fill "OutputData" object. I do not want to change it to XmlSerializer now :-(.

示例代码:

    // Gets the root element decendants
        var elementRootData = xDocument.Descendants("RootData");

        var xElements = elementRootData as IList<XElement> ?? elementRootData.ToList();


   // Read first leaf node "ProfileResult"
        var passResult = from xElement in xElements.Descendants("PassResult")
            select new
            {

                FirstOne = xElement.Element("FirstOne").GetValue(),
                SecondOne = xElement.Element("SecondOne").GetValue(),
                IsMale = xElement.Element("IsMale").GetValue()
            };


    // Read second leaf note
        var testResult = from xElement in xElements.Descendants("TestResult")
            select new
            {

                MarkOne = xElement.Element("MarkOne").GetValue(),
                MarkTwo = xElement.Element("MarkTwo").GetValue(),
                Slope = xElement.Element("Slope").GetValue()
            };


    // Update OutputData object
        var parseOutputData = new OutputData();

    foreach (var result in passResult)
        {
            parseOutputData.FirstOne = result.FirstOne;
            parseOutputData.SecondOne = result.SecondOne;
            parseOutputData.IsMale = result.IsMale.Equals("True");
        }

        foreach (var result in testResult)
        {
            parseOutputData.MarkOne = double.Parse(result.MarkOne);
            parseOutputData.MarkTwo = double.Parse(result.MarkTwo);
            parseOutputData.Slope = double.Parse(result.Slope);
        }



我要多写一些这样的代码来填补像ToneTestResult其他元素数据,QuestionnaireResult等
能有人用示例代码提示?

I have to write some more code like this to fill other elements data like ToneTestResult, QuestionnaireResult etc.Can someone suggest with a sample code?

最好的问候,

推荐答案

鉴于你的XML是微小的,你可能不必担心性能太多。你可以做整个事情一气呵成,利用内置的显式转换:

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()
};



顺便说一句,后裔永远不会返回任何实施的IList<的XElement方式> ,所以你绝对可以删除

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

这篇关于XML解析 - 代码重构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:11