从使用 linq 提取的 xml 中获取错误的文件名、cName、cValue

问题描述 投票:0回答:1

我有一个项目,它读取每个构建区域的预期构建结果的 xml,并与构建日志进行比较以查看构建是否通过。 xml 看起来读取不正确。有谁知道 linq 有什么问题导致检索到的 xml 不正确吗?

我一直在看这些例子: linq linq

但是,当我在使用它之前打印并检查它时,它得到了错误的cName和cValue。另外,它总是给出 filename1.cs:

错误:

19:00
build_machine='mach31', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\R\filename1.cs', field='Status', comparison='equal', value='Success'
build_machine='mach31', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\R\filename1.cs', field='Status', comparison='equal', value='Success'
build_machine='mach31', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\R\filename1.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinISProjectVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'

应该是:

19:00
build_machine='mach31', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\R\filename1.cs', field='Status', comparison='equal', value='Success'
build_machine='mach31', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\R\filename2.cs', field='Status', comparison='equal', value='Success'
build_machine='mach31', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\R\filename3.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename4.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename5.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename6.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\D\filename7.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\K\filename8.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\K\filename9.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\K\filename11.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename22.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinFileVersionStats', condition name='VersionFile', condition value = '\\view\Build_NightlyDeveloper\K\filename33.cs', field='Status', comparison='equal', value='Success'
build_machine='mach46', process_name='SpinISProjectVersionStats', condition name='ProductName', condition value = 'P1', field='Status', comparison='equal', value='Success'

...

到目前为止我有这个代码:

void ReadXml()
{
    XmlDocument xml = new XmlDocument();
    string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string newPath = Path.GetFullPath(Path.Combine(path, @"..\.."));
    string finalPathXml = Path.GetFullPath(Path.Combine(newPath, @"BuildVerificationBuildAttributes.xml"));

    //linq 
    XDocument xmlDoc1 = XDocument.Load(finalPathXml);
    XElement buildVerificationElement = xmlDoc1.Element("BuildVerification");
    IEnumerable<XElement> buildElements = buildVerificationElement.Elements("build");
    IEnumerable<XElement> buildMachines = buildElements.Elements("BuildMachine");

    //get just the times from the xml
    var codeFreezeTime = xmlDoc1.Descendants("codeFreezeTime").First()?.Value;
    Console.WriteLine(codeFreezeTime);

    IEnumerable<XElement> processes = buildMachines.Elements("Process");
    IEnumerable<XElement> processNames = processes.Elements("ProcessName");
    IEnumerable<XElement> conditions = processNames.Elements("Conditions");
    var found = xmlDoc1.XPathEvaluate("/BuildVerification/build/BuildMachine/Process/ProcessName/Conditions") as IEnumerable<object>;

    var results =
        xmlDoc1.Descendants("build")
        .SelectMany(x => x.Descendants("Process")
        .SelectMany(y => y.Descendants("SuccessCriteria")
        .Select(z => new
        {
            buildMach = (string)x.Element("BuildMachine"),
            p1 = (string)y.Element("ProcessName"),
            cName = (string)x.Descendants("Condition").FirstOrDefault().Attribute("name"),
            cValue = (string)x.Descendants("Condition").FirstOrDefault().Attribute("value"),
            f1 = (string)z.Element("field"),
            c1 = (string)z.Element("comparison"),
            v1 = (string)z.Element("value")
        }))).ToList();
    foreach (var cur in results)
    {
        Console.WriteLine("build_machine='{0}', process_name='{1}', condition name='{2}', condition value = '{3}', field='{4}', comparison='{5}', value='{6}'",
            cur.buildMach, cur.p1, cur.cName, cur.cValue, cur.f1, cur.c1, cur.v1);

        //get log data out to compare
        if((cur.buildMach == "mach46") && (cur.p1 == "SpinISProjectVersionStats"))
        {
            Console.WriteLine("here"); //*at this point I see cur.cName=VersionFile which is wrong
        }
        //ParseLogFile(codeFreezeTime/*, versionChangeTime*/, cur.buildMach, cur.p1, cur.cName, cur.cValue, cur.f1, cur.c1, cur.v1);
    }

}

这就是我的 xml 的样子:

<?xml version="1.0" encoding="UTF-8"?>
<BuildVerification>
   <codeFreezeTime>19:00</codeFreezeTime>
   <build>
       <BuildMachine>mach31</BuildMachine>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\R\filename1.cs">
                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison> <!--can't use >= here -->
                            <value>Success</value>
                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\R\filename2.cs">
                            <SuccessCriteria>
                                <field>Status</field>
                                <comparison>equal</comparison>
                                <value>Success</value>

                            </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\R\filename3.h">
                        <SuccessCriteria>
                        <field>Status</field>
                        <comparison>equal</comparison>
                        <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>

    </build>
    
    
    <build>
    <BuildMachine>mach46</BuildMachine>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\filename4.cs">
                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison> 
                            <value>Success</value>
                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\fiename5.h">
                            <SuccessCriteria>
                                <field>Status</field>
                                <comparison>equal</comparison>
                                <value>Success</value>

                            </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\filename6.cs">

                        <SuccessCriteria>
                        <field>Status</field>
                        <comparison>equal</comparison>
                        <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\D\filename7.cs">
                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison> 
                            <value>Success</value>
                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\K\filename8.cs">

                            <SuccessCriteria>
                                <field>Status</field>
                                <comparison>equal</comparison>
                                <value>Success</value>

                            </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\K\filename9.h">

                        <SuccessCriteria>
                        <field>Status</field>
                        <comparison>equal</comparison>
                        <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        
                    
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\K\filename11.cs">
                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison> <!--can't use >= here -->
                            <value>Success</value>
                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\filename22.cs">

                            <SuccessCriteria>
                                <field>Status</field>
                                <comparison>equal</comparison>
                                <value>Success</value>

                            </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinFileVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='VersionFile' value="\\view\Build_NightlyDeveloper\K\filename33.cs">

                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison>
                            <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        
        
        <Process>
            <ProcessName>SpinISProjectVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='ProductName' value="P1">

                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison>
                            <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinISProjectVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='ProductName' value="P2">

                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison>
                            <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>SpinISProjectVersionStats</ProcessName>
                <Conditions>
                    <Condition
                        name='ProductName' value="P3">

                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison>
                            <value>Success</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>GetSWStats</ProcessName>
                <Conditions>
                    <Condition
                        name='DestinationPath' value="D:\Builds\Retail\nightly">

                        <SuccessCriteria>
                            <field>UpdateRequired</field>
                            <comparison>equal</comparison>
                            <value>YES</value>

                        </SuccessCriteria>
    
                    </Condition>
                    <Condition
                        name='DestinationPath' value="D:\Builds\Retail\nightly">

                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison>
                            <value>Successful</value>

                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        
        <Process>
            <ProcessName>ParseISLogStats</ProcessName>
                <Conditions>
                    <Condition
                        name='BuildProject' value="RetailInstallerBuild">
                        <SuccessCriteria>
                            <field>Projects</field>
                            <comparison>equal</comparison> 
                            <value>33</value>
                        </SuccessCriteria>
                    </Condition>
                    <Condition
                        name='BuildProject' value="RetailInstallerBuild">
                        <SuccessCriteria>
                            <field>Projects</field>
                            <comparison>equal</comparison> 
                            <value>8</value>
                        </SuccessCriteria>
                        <SuccessCriteria>
                            <field>Status</field>
                            <comparison>equal</comparison> 
                            <value>Success</value>
                        </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
        <Process>
            <ProcessName>UpdateBuildArea-Stats</ProcessName>
                <Conditions>
                    <Condition
                        name='DestPath' value="\\mach62\Builds\nightly">

                            <SuccessCriteria>
                                <field>Dir</field>
                                <comparison>equal</comparison>
                                <value>Retail</value>

                            </SuccessCriteria>
                    </Condition>
                    <Condition
                        name='DestPath' value="\\mach62\Builds\nightly">

                            <SuccessCriteria>
                                <field>eSecs</field>
                                <comparison>greaterThan</comparison>
                                <value>0</value>

                            </SuccessCriteria>
                    </Condition>
                </Conditions>
        </Process>
    </build>
</BuildVerification>
c# xml linq
1个回答
0
投票

SuccessCriteria
节点开始,通过
Parent
属性向上导航以获取
cName
cValue
的值。

var results =
    xmlDoc1.Descendants("build")
    .SelectMany(x => x.Descendants("Process")
    .SelectMany(y => y.Descendants("SuccessCriteria")
    .Select(z => new
    {
        cName = (string)z.Parent.Parent.Parent.Element("ProcessName"),
        cValue = (string)z.Parent.Attribute("value")
        
        // Remaining properties
        
    }))).ToList();

© www.soinside.com 2019 - 2024. All rights reserved.