综合运营商

时间:2019-03-11 16:03:24

标签: c# .net xml

这里有以下XML数据:

<RepairOrderEvent>
    <EventType>Advisor - Repair Facility</EventType>
    <EventDateTime>2019-01-07T11:18:38.8756279-06:00</EventDateTime>
    <EventNotes>Weld-through primer is not included.; Rule Type: Estimate Line; Score: 1; Variance: 0.0; UniqueSequenceNum: 9</EventNotes>
</RepairOrderEvent>
<RepairOrderEvent>
    <EventType>Advisor - Shared Guidelines</EventType>
    <EventDateTime>2019-01-07T11:18:38.8756279-06:00</EventDateTime>    
    <EventNotes>FAF(Part):More cost effective approved Bumper Cover avail, SELECT or DOCUMENT CA; Score: 11; Variance: 402.14; UniqueSequenceNum: 2; Insurance Score: 250</EventNotes>
</RepairOrderEvent>
<RepairOrderEvent>
    <EventType>Advisor - Shared Guidelines</EventType>
    <EventDateTime>2019-01-07T11:18:38.8756279-06:00</EventDateTime>
    <EventNotes>FAF(Part):More cost effective approved Cert Headlamp avail, SELECT or DOC; Score: 11; Variance: 164.04; UniqueSequenceNum: 6; Insurance Score: 250</EventNotes>
</RepairOrderEvent>    
<RepairOrderEvent>
    <EventType>Advisor - Shared Guidelines</EventType>
    <EventDateTime>2019-01-07T11:18:38.8756279-06:00</EventDateTime>
    <EventNotes>FAF(Profile): LKQ Markup Exceeds Rate Guide; Score: 11; Variance: 24.8; UniqueSequenceNum: 0; Insurance Score: 10</EventNotes>
</RepairOrderEvent>

正如你们所看到的,既有得分,也有保险得分。我只对“分数”及其旁边的数字感兴趣。
我需要创建一个运算符,该运算符将获取分数的值并吐出所有分数的总和(不包括保险分数)。

在处理方面,我已经加载了XML文档,并且在解析数据时遇到了麻烦。最终目的是能够读取第一次出现的每一行字符串的其余部分,然后找到总分。

以下是我到目前为止的内容:

case "Aggregate": 
{
    XmlDocument currentWorkfileDoc = new XmlDocument();
    currentWorkfileDoc.LoadXml(currentWorkfileXML);

    foreach (XmlNode row in currentWorkfileDoc.SelectNodes("EventNotes"))
    {
        var rowName = row.SelectNodes("EventNote"); 
    }

    if (result)
        return result;
}
break;

1 个答案:

答案 0 :(得分:0)

如果您可以使用XDocument,则可以执行以下操作:

var currentWorkfileDoc = XDocument.Load(memoConfigFile);
var sum = (from item in currentWorkfileDoc.Elements("EventNotes")
            select Regex.Split(item.Value, @"\D+")[0]).Sum(x => int.Parse(x)); 

或根据需要尝试使用XmlDocument使用以下代码:

XmlDocument currentWorkfileDoc = new XmlDocument();
currentWorkfileDoc.LoadXml(currentWorkfileXML); 
int sum = 0;
XmlNodeList elemList = currentWorkfileDoc.GetElementsByTagName("EventNotes");
for (int i = 0; i < elemList.Count; i++)
    sum += int.Parse(Regex.Split(elemList[i].Value, @"\D+")[0]);

更新:

我尝试过Regex会找到所有数字作为字符串数组,但是数组的第一个索引是"",您应该像Regex.Split(elemList[i].Value, @"\D+")[1]那样将它作为第二个索引。
另一方面,如果在第一个score之前可能有数字,则可以将元素基数Score:的字符串和substring的第二个分割索引的字符串拆分为第一个;,例如:

foreach (var element in currentWorkfileDoc1.Descendants("EventNotes"))
{
    string[] strPart = element.Value.Split(new string[] { "Score:" }, StringSplitOptions.None);
    sum += int.Parse(strPart[1].Substring(0, strPart[1].IndexOf(';')));
}

注意:
您的xml文档没有任何root元素,因此无法将其加载到XmlDocument中,我尝试了第二次为您的XML编写的代码,一切正常

<root>
<RepairOrderEvent>
    <EventType>Advisor - Repair Facility</EventType>
    <EventDateTime>2019-01-07T11:18:38.8756279-06:00</EventDateTime>
    <EventNotes>Weld-through primer is not included.; Rule Type: Estimate Line; Score: 1; Variance: 0.0; UniqueSequenceNum: 9</EventNotes>
</RepairOrderEvent> 
</root>