XML & .NET - 仅对最深节点中的元素进行排序
Posted
技术标签:
【中文标题】XML & .NET - 仅对最深节点中的元素进行排序【英文标题】:XML & .NET - Sort only elements in the deepest nodes 【发布时间】:2021-11-12 16:30:08 【问题描述】:我遇到了这个挑战,我碰壁了,如果没有一些额外的知识就无法进步:
我有这个结构:
我只想对最深节点中的元素进行排序。上层节点的数量和名称差异很大。
我的问题是节点以名称
这是上述示例的 XML 代码:
<!--xml version="1.0" encoding="ISO-8859-1"-->
<ToolsItemsManager Version="4" Istoolkit="1" Machine="">
<Components>
<CompTool Ver="2" ID="1" Name="Cutters" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="2" ID="5" ExtrId="12181715949" Name="SubNode 1" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="2" ID="6" ExtrId="121817151010" Name="SubNode 2" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="2" ID="7" ExtrId="121817151015" Name="SubNode 3" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="2" ID="8" ExtrId="121817151020" Name="SubNode 4" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="2" ID="9" ExtrId="121817151044" Name="SubNode 5" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="2" ID="10" ExtrId="121817151049" Name="SubNode 6" Type="6" FolderOf="1" Description="">
<Components>
<CompTool Ver="1" ID="11" ExtrId="121817151113" Name="2222" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="1" ShapeType="0" IsMilling="-1" ToolType="2" InsertOnly="-1">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="45"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="0" NumFlutes="2" Rough="0" IsInputSA="-1">
<LenParams>
<AD Units="0" Val="6"/>
<CL Units="0" Val="24"/>
<D Units="0" Val="6"/>
<SD Units="0" Val="6"/>
<SL Units="0" Val="30"/>
<TL Units="0" Val="80"/>
</LenParams>
<Params>
<HelicalAngle Units="0" Val="45"/>
<SA Units="0" Val="0"/>
</Params>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
<CuttingConditionsList Default="1">
<CC Ver="1" InchMode="0" Precision="3" Name="Default" Id="1" OperationType="4" WorkMaterial="" Comment="">
<MillingFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="2" Normal="0.1" CheckFinish="-1" Finish="0.1" Z="0.01" ZOnlyPenetration="0" Link="200" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="70" CheckFinish="-1" Finish="70" Normal="0" Limit="0" Dir="0"/>
</MillingFeedSpin>
<TurningFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="4" Normal="0.1" CheckFinish="-1" Finish="0.05" Z="0" ZOnlyPenetration="0" Link="100" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="0" CheckFinish="-1" Finish="90" Normal="70" Limit="1000" Dir="0"/>
</TurningFeedSpin>
<TurretCoolant Ver="1" Device="2" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
<MachineCoolant Ver="1" Device="0" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
</CC>
</CuttingConditionsList>
<Material Ver="1" Material="0" PercentMaterial="100" CheckPercent="0"/>
<CutPoints>
<CutPoint Ver="5" InchMode="0" Name="Cutting point" Id="1" OperationType="0" RadIndx="0" Description="" Hyperlink="" Color="62207" ForCut="-1" LinkToDiamCutter="-1" LockH="-1" CuttingCondition="1" OffsetNum="1" OffsetIndex="1" Radius="3" OriginNum="0" AutoOriginNum="-1" MeasureOrien="0" Flip="0" DTAxisVal="0" NosePointX="0" NosePointY="0" NosePointZ="0" CheckToolBreakage="0" CheckToolLength="0" CheckToolRadius="0" LengthTolerance="0.5" RadiusTolerance="0.1" UsedInHR="0" UsedInPart="0"/>
</CutPoints>
</CompTool>
<CompTool Ver="1" ID="12" ExtrId="121817151124" Name="1111" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="1" ShapeType="0" IsMilling="-1" ToolType="2" InsertOnly="-1">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="45"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="0" NumFlutes="2" Rough="0" IsInputSA="-1">
<LenParams>
<AD Units="0" Val="6"/>
<CL Units="0" Val="24"/>
<D Units="0" Val="6"/>
<SD Units="0" Val="6"/>
<SL Units="0" Val="30"/>
<TL Units="0" Val="80"/>
</LenParams>
<Params>
<HelicalAngle Units="0" Val="45"/>
<SA Units="0" Val="0"/>
</Params>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
<CuttingConditionsList Default="1">
<CC Ver="1" InchMode="0" Precision="3" Name="Default" Id="1" OperationType="4" WorkMaterial="" Comment="">
<MillingFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="2" Normal="0.1" CheckFinish="-1" Finish="0.1" Z="0.01" ZOnlyPenetration="0" Link="200" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="70" CheckFinish="-1" Finish="70" Normal="0" Limit="0" Dir="0"/>
</MillingFeedSpin>
<TurningFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="4" Normal="0.1" CheckFinish="-1" Finish="0.05" Z="0" ZOnlyPenetration="0" Link="100" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="0" CheckFinish="-1" Finish="90" Normal="70" Limit="1000" Dir="0"/>
</TurningFeedSpin>
<TurretCoolant Ver="1" Device="2" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
<MachineCoolant Ver="1" Device="0" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
</CC>
</CuttingConditionsList>
<Material Ver="1" Material="0" PercentMaterial="100" CheckPercent="0"/>
<CutPoints>
<CutPoint Ver="5" InchMode="0" Name="Cutting point" Id="1" OperationType="0" RadIndx="0" Description="" Hyperlink="" Color="62207" ForCut="-1" LinkToDiamCutter="-1" LockH="-1" CuttingCondition="1" OffsetNum="1" OffsetIndex="1" Radius="3" OriginNum="0" AutoOriginNum="-1" MeasureOrien="0" Flip="0" DTAxisVal="0" NosePointX="0" NosePointY="0" NosePointZ="0" CheckToolBreakage="0" CheckToolLength="0" CheckToolRadius="0" LengthTolerance="0.5" RadiusTolerance="0.1" UsedInHR="0" UsedInPart="0"/>
</CutPoints>
</CompTool>
<CompTool Ver="1" ID="13" ExtrId="121817151133" Name="5555" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="1" ShapeType="0" IsMilling="-1" ToolType="2" InsertOnly="-1">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="45"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="0" NumFlutes="2" Rough="0" IsInputSA="-1">
<LenParams>
<AD Units="0" Val="6"/>
<CL Units="0" Val="24"/>
<D Units="0" Val="6"/>
<SD Units="0" Val="6"/>
<SL Units="0" Val="30"/>
<TL Units="0" Val="80"/>
</LenParams>
<Params>
<HelicalAngle Units="0" Val="45"/>
<SA Units="0" Val="0"/>
</Params>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
<CuttingConditionsList Default="1">
<CC Ver="1" InchMode="0" Precision="3" Name="Default" Id="1" OperationType="4" WorkMaterial="" Comment="">
<MillingFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="2" Normal="0.1" CheckFinish="-1" Finish="0.1" Z="0.01" ZOnlyPenetration="0" Link="200" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="70" CheckFinish="-1" Finish="70" Normal="0" Limit="0" Dir="0"/>
</MillingFeedSpin>
<TurningFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="4" Normal="0.1" CheckFinish="-1" Finish="0.05" Z="0" ZOnlyPenetration="0" Link="100" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="0" CheckFinish="-1" Finish="90" Normal="70" Limit="1000" Dir="0"/>
</TurningFeedSpin>
<TurretCoolant Ver="1" Device="2" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
<MachineCoolant Ver="1" Device="0" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
</CC>
</CuttingConditionsList>
<Material Ver="1" Material="0" PercentMaterial="100" CheckPercent="0"/>
<CutPoints>
<CutPoint Ver="5" InchMode="0" Name="Cutting point" Id="1" OperationType="0" RadIndx="0" Description="" Hyperlink="" Color="62207" ForCut="-1" LinkToDiamCutter="-1" LockH="-1" CuttingCondition="1" OffsetNum="1" OffsetIndex="1" Radius="3" OriginNum="0" AutoOriginNum="-1" MeasureOrien="0" Flip="0" DTAxisVal="0" NosePointX="0" NosePointY="0" NosePointZ="0" CheckToolBreakage="0" CheckToolLength="0" CheckToolRadius="0" LengthTolerance="0.5" RadiusTolerance="0.1" UsedInHR="0" UsedInPart="0"/>
</CutPoints>
</CompTool>
<CompTool Ver="1" ID="14" ExtrId="121817151144" Name="3333" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="1" ShapeType="0" IsMilling="-1" ToolType="2" InsertOnly="-1">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="45"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="0" NumFlutes="2" Rough="0" IsInputSA="-1">
<LenParams>
<AD Units="0" Val="6"/>
<CL Units="0" Val="24"/>
<D Units="0" Val="6"/>
<SD Units="0" Val="6"/>
<SL Units="0" Val="30"/>
<TL Units="0" Val="80"/>
</LenParams>
<Params>
<HelicalAngle Units="0" Val="45"/>
<SA Units="0" Val="0"/>
</Params>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
<CuttingConditionsList Default="1">
<CC Ver="1" InchMode="0" Precision="3" Name="Default" Id="1" OperationType="4" WorkMaterial="" Comment="">
<MillingFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="2" Normal="0.1" CheckFinish="-1" Finish="0.1" Z="0.01" ZOnlyPenetration="0" Link="200" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="70" CheckFinish="-1" Finish="70" Normal="0" Limit="0" Dir="0"/>
</MillingFeedSpin>
<TurningFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="4" Normal="0.1" CheckFinish="-1" Finish="0.05" Z="0" ZOnlyPenetration="0" Link="100" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="0" CheckFinish="-1" Finish="90" Normal="70" Limit="1000" Dir="0"/>
</TurningFeedSpin>
<TurretCoolant Ver="1" Device="2" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
<MachineCoolant Ver="1" Device="0" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
</CC>
</CuttingConditionsList>
<Material Ver="1" Material="0" PercentMaterial="100" CheckPercent="0"/>
<CutPoints>
<CutPoint Ver="5" InchMode="0" Name="Cutting point" Id="1" OperationType="0" RadIndx="0" Description="" Hyperlink="" Color="62207" ForCut="-1" LinkToDiamCutter="-1" LockH="-1" CuttingCondition="1" OffsetNum="1" OffsetIndex="1" Radius="3" OriginNum="0" AutoOriginNum="-1" MeasureOrien="0" Flip="0" DTAxisVal="0" NosePointX="0" NosePointY="0" NosePointZ="0" CheckToolBreakage="0" CheckToolLength="0" CheckToolRadius="0" LengthTolerance="0.5" RadiusTolerance="0.1" UsedInHR="0" UsedInPart="0"/>
</CutPoints>
</CompTool>
<CompTool Ver="1" ID="15" ExtrId="121817151154" Name="2222" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="1" ShapeType="0" IsMilling="-1" ToolType="2" InsertOnly="-1">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="45"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="0" NumFlutes="2" Rough="0" IsInputSA="-1">
<LenParams>
<AD Units="0" Val="6"/>
<CL Units="0" Val="24"/>
<D Units="0" Val="6"/>
<SD Units="0" Val="6"/>
<SL Units="0" Val="30"/>
<TL Units="0" Val="80"/>
</LenParams>
<Params>
<HelicalAngle Units="0" Val="45"/>
<SA Units="0" Val="0"/>
</Params>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
<CuttingConditionsList Default="1">
<CC Ver="1" InchMode="0" Precision="3" Name="Default" Id="1" OperationType="4" WorkMaterial="" Comment="">
<MillingFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="2" Normal="0.1" CheckFinish="-1" Finish="0.1" Z="0.01" ZOnlyPenetration="0" Link="200" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="70" CheckFinish="-1" Finish="70" Normal="0" Limit="0" Dir="0"/>
</MillingFeedSpin>
<TurningFeedSpin Ver="1" InchMode="0" RiskLevel="5">
<Feeds Ver="1" InchMode="0" Units="4" Normal="0.1" CheckFinish="-1" Finish="0.05" Z="0" ZOnlyPenetration="0" Link="100" LeadIn="100" LeadOut="100"/>
<Spins Ver="1" InchMode="0" Units="0" Rate="0" CheckFinish="-1" Finish="90" Normal="70" Limit="1000" Dir="0"/>
</TurningFeedSpin>
<TurretCoolant Ver="1" Device="2" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
<MachineCoolant Ver="1" Device="0" Flood="0" FloodHP="0" FloodLP="0" FloodVal="0" Mist="0" MistHP="0" MistLP="0" MistVal="0" ThroughTool="0" ThroughToolHP="0" ThroughToolMP="0" ThroughToolLP="0" ThroughToolVal="0" MinQuantLubr="0" MinQuantLubrVal="0" AirActive="0" BedRinsing="0" AirThroughSpindle="0"/>
</CC>
</CuttingConditionsList>
<Material Ver="1" Material="0" PercentMaterial="100" CheckPercent="0"/>
<CutPoints>
<CutPoint Ver="5" InchMode="0" Name="Cutting point" Id="1" OperationType="0" RadIndx="0" Description="" Hyperlink="" Color="62207" ForCut="-1" LinkToDiamCutter="-1" LockH="-1" CuttingCondition="1" OffsetNum="1" OffsetIndex="1" Radius="3" OriginNum="0" AutoOriginNum="-1" MeasureOrien="0" Flip="0" DTAxisVal="0" NosePointX="0" NosePointY="0" NosePointZ="0" CheckToolBreakage="0" CheckToolLength="0" CheckToolRadius="0" LengthTolerance="0.5" RadiusTolerance="0.1" UsedInHR="0" UsedInPart="0"/>
</CutPoints>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
<CompTool Ver="2" ID="2" Name="Shanks" Type="6" FolderOf="5" Description=""/>
<CompTool Ver="2" ID="3" Name="Adaptors and Holders" Type="6" FolderOf="2" Description="">
<Components>
<CompTool Ver="2" ID="28" ExtrId="121817151442" Name="SubNode 1" Type="6" FolderOf="2" Description="">
<Components>
<CompTool Ver="2" ID="29" ExtrId="121817151446" Name="SubNode 2" Type="6" FolderOf="2" Description="">
<Components>
<CompTool Ver="1" ID="30" ExtrId="121817151459" Name="1234" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="2" ShapeType="2" BaseGeometry="0">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="0"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="2" FileName="" FromImprtDir="0">
<MassCenter x="-999999999.99999" y="-999999999.99999" z="-999999999.99999"/>
<Box3DB x="-1.79769313486232E+308" y="-1.79769313486232E+308" z="-1.79769313486232E+308"/>
<Box3DT x="1.79769313486232E+308" y="1.79769313486232E+308" z="1.79769313486232E+308"/>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
</CompTool>
<CompTool Ver="1" ID="31" ExtrId="12181715155" Name="3456" CatalogNum="" Description="" Hyperlink="" HasCoolantHole="0" Price="0" Mass="0" Quantity="1" MinQuantity="1" Type="2" ShapeType="2" BaseGeometry="0">
<Mounting ConnectToJoint="">
<MountingX x="1" y="0" z="0"/>
<MountingY x="0" y="1" z="0"/>
<MountingZ x="0" y="0" z="1"/>
<MountingPos x="0" y="0" z="0"/>
<MountingAngles x="0" y="0" z="0"/>
</Mounting>
<Shape Ver="4" InchMode="0" Name="" ShapeType="2" FileName="" FromImprtDir="0">
<MassCenter x="-999999999.99999" y="-999999999.99999" z="-999999999.99999"/>
<Box3DB x="-1.79769313486232E+308" y="-1.79769313486232E+308" z="-1.79769313486232E+308"/>
<Box3DT x="1.79769313486232E+308" y="1.79769313486232E+308" z="1.79769313486232E+308"/>
</Shape>
<Joints>
<Joint Ver="2" InchMode="0" Name="Joint_1" ForPartMounting="0" DriveUnitType="3">
<PlugIn>
<PlugInX x="1" y="0" z="0"/>
<PlugInY x="0" y="1" z="0"/>
<PlugInZ x="0" y="0" z="1"/>
<PlugInPos x="0" y="0" z="0"/>
<PlugInAngles x="0" y="0" z="0"/>
</PlugIn>
<Filter Ver="1"/>
</Joint>
</Joints>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
</Components>
</CompTool>
<CompTool Ver="2" ID="4" Name="Fixtures" Type="6" FolderOf="4" Description=""/>
</Components>
</ToolsItemsManager>
这是我用来尝试对其进行排序的一段代码,但它没有按我的预期工作:
private static XElement Sort(
XElement element,
int level,
string attribute,
int sortAttributes)
XElement newElement = new XElement(element.Name,
from child in element.Elements()
orderby
(child.Ancestors().Count() > level)
? (
(child.HasAttributes
&& !string.IsNullOrEmpty(attribute)
&& child.Attribute(attribute) != null)
? child.Attribute(attribute).Value.ToString()
: child.Name.ToString()
)
: "" //End of the orderby clause
select Sort(child, level, attribute, sortAttributes));
if (element.HasAttributes)
switch (sortAttributes)
case 0: //None
foreach (XAttribute attrib in element.Attributes())
newElement.SetAttributeValue(attrib.Name, attrib.Value);
break;
case 1: //Ascending
foreach (XAttribute attrib in element.Attributes()
.OrderBy(a => a.Name.ToString()))
newElement.SetAttributeValue(attrib.Name, attrib.Value);
break;
case 2: //Decending
foreach (XAttribute attrib in element.Attributes()
.OrderByDescending(
a => a.Name.ToString()))
newElement.SetAttributeValue(attrib.Name, attrib.Value);
break;
default:
break;
return newElement;
谁能在这里指出我正确的方向?
非常感谢!
【问题讨论】:
您有代表您的 xml 模型的 .NET 类吗?如果是这样,使用序列化将是非常微不足道的 此外,您的图像显示的 Xml 结构比您的代码更简单。我可以根据图像中的 Xml 给你一些东西,但我没有根据代码输入所有内容。也许我们只是看不到图像中的安装、关节、形状等 你是对的。该图像未显示我要排序的这些元素的其他属性。老实说,我认为我无法画出更好的图像,因为生成这些 XML 的应用程序的 UI 确实很密集。 【参考方案1】:使用递归。
有这样的模式:
For each element in root
Sort(element)
In Sort
if(element has child nodes with name starting with 'CompTool')
Call Sort on each of them
else
Your at the base of one node tree, and sort it.
只是因为你有一个Components
标记,所以它有点困难。不清楚每个 CompTool 中是否有多个 Components
标签,还是只有一个。
无论哪种情况,您都在检查Components
是否有子CompTool
s。并致电Sort(on each CompTool)
。
【讨论】:
以上是关于XML & .NET - 仅对最深节点中的元素进行排序的主要内容,如果未能解决你的问题,请参考以下文章
将包含 & 的 XML 值节点保存到数据库中的正确方法是啥?