2016-02-04 78 views
0

我試圖對包含數千個節點的大型XML文件進行排序,並且爲了排序所有節點的共同點,還有一個名爲「ID」的屬性。排序密集XML文件的節點

該屬性之前的元素類型有所不同:它可以有多個名稱,例如「Apt」,「CentreDrill」,「Reamer」,「Cylinder」,「Block」,「Model」,「Sweep」錐體「,」探針「和其他一些。這讓我很難找出一種算法,可以進行正確的排序,而不管屬性前的名稱如何。

我不希望與outter元素的順序混亂,那就是我想要的元素「參數米爾斯」「參數鑽頭」「工具架」「探頭觸筆「,」拋光器「保持相同的順序。我想對內部元素進行排序,如下圖所示。

Original XML in the native applicationSorted XML in the native application

原始XML內容:精簡版(287線)

<?xml version="1.0"?> 
    <Tools> 
    <Tool ID="Parametric Mills" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Teeth>2</Teeth> 
     <Type>Milling</Type> 
     <Cutter> 
     <Apt ID="10000" ParentID="" Type="FLAT END" > 
      <D>12.7</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>0</A> 
      <B>0</B> 
      <H>88.9</H> 
      <StickoutLength>63.5</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="2" ParentID="" Type="BALL END" > 
      <D>12.7</D> 
      <R>6.35</R> 
      <E>0</E> 
      <F>6.35</F> 
      <A>0</A> 
      <B>0</B> 
      <H>88.9</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="33" ParentID="" Type="TAPERED BULL NOSE" > 
      <D>12.7</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>0</A> 
      <B>5</B> 
      <H>88.9</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <ShankDiameter>12.7</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="3000" ParentID="" Type="BULL NOSE" > 
      <D>12.7</D> 
      <R>2</R> 
      <E>4.35</E> 
      <F>2</F> 
      <A>0</A> 
      <B>0</B> 
      <H>88.9</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-114.3</Z> 
     </DrivenPoint> 
    </Tool> 
    <Tool ID="Parametric Drills" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Teeth>2</Teeth> 
     <Type>HoleMaking</Type> 
     <Cutter> 
     <Apt ID="5000" ParentID="" Type="DRILL" > 
      <D>9.525</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>31</A> 
      <B>0</B> 
      <H>101.6</H> 
      <StickoutLength>76.2</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>63.5</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Reamer ID="111" ParentID="" DRILLMILL="FALSE" Type="Reamer" > 
      <ChamferLength>0.5</ChamferLength> 
      <D>12</D> 
      <H>100</H> 
      <ShankDiameter>12</ShankDiameter> 
      <StickoutLength>0</StickoutLength> 
      <SpindleDirection>CW</SpindleDirection> 
      <MAXIMUMMATERIALREMOVAL>0</MAXIMUMMATERIALREMOVAL> 
      <FluteLength>63.5</FluteLength> 
      <Alternate>off</Alternate> 
     </Reamer> 
     <CenterDrill ID="3" ParentID="" DRILLMILL="FALSE" Type="CenterDrill" > 
      <ChamferAngle>60</ChamferAngle> 
      <H>50</H> 
      <PilotAngle>31</PilotAngle> 
      <D>3</D> 
      <PilotLength>3</PilotLength> 
      <ShankDiameter>12</ShankDiameter> 
      <StickoutLength>0</StickoutLength> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>11.69552</FluteLength> 
      <Alternate>off</Alternate> 
     </CenterDrill> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-127</Z> 
     </DrivenPoint> 
    </Tool> 
    <Tool ID="Tool Holders" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Teeth>2</Teeth> 
     <Type>Turning</Type> 
     <Holder> 
     <Block ID="455" ParentID=""> 
      <Width>25.4</Width> 
      <Length>25.4</Length> 
      <Height>101.6</Height> 
      <NoSpin>0</NoSpin> 
      <Alternate>off</Alternate> 
     </Block> 
     <Cylinder ID="1" ParentID=""> 
      <Radius>50</Radius> 
      <Height>10</Height> 
      <NoSpin>0</NoSpin> 
      <Alternate>off</Alternate> 
     </Cylinder> 
     <Cone ID="50000" ParentID=""> 
      <ButtomRadius>30</ButtomRadius> 
      <TopRadius>10</TopRadius> 
      <Height>50</Height> 
      <NoSpin>0</NoSpin> 
      <Alternate>off</Alternate> 
     </Cone> 
     </Holder> 
    </Tool> 
    <Tool ID="Probe Stylus" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Type>Probing</Type> 
     <Cutter> 
     <Probe ID="11500" ParentID="" Type="Standard"> 
      <SphereDiameter>5.00126</SphereDiameter> 
      <Height>101.6</Height> 
      <StemDiameter>2.99974</StemDiameter> 
      <Length>101.6</Length> 
      <MaxRPM>0</MaxRPM> 
      <StemRGB>-4144960</StemRGB> 
      <StickoutLength>76.2</StickoutLength> 
      <Alternate>off</Alternate> 
     </Probe> 
     <Probe ID="10455" ParentID="" Type="Cylinder"> 
      <SphereDiameter>10</SphereDiameter> 
      <Height>10</Height> 
      <StemDiameter>3</StemDiameter> 
      <Length>50</Length> 
      <MaxRPM>0</MaxRPM> 
      <StemRGB>-4144960</StemRGB> 
      <StickoutLength>0</StickoutLength> 
      <Alternate>off</Alternate> 
     </Probe> 
     <Probe ID="11111" ParentID="" Type="Disk"> 
      <SphereDiameter>20</SphereDiameter> 
      <Height>3</Height> 
      <Height2>5</Height2> 
      <Diameter2>3</Diameter2> 
      <PinOffset>8</PinOffset> 
      <StemDiameter>6</StemDiameter> 
      <Length>50</Length> 
      <MaxRPM>0</MaxRPM> 
      <StemRGB>-4144960</StemRGB> 
      <StickoutLength>0</StickoutLength> 
      <Alternate>off</Alternate> 
     </Probe> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-127</Z> 
     </DrivenPoint> 
    </Tool> 
    <Tool ID="Polishers" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Type>Polisher</Type> 
     <Cutter> 
     <Apt ID="50" ParentID="" Type="FLAT END" PolishType="Polisher"> 
      <D>38.1</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>0</A> 
      <B>0</B> 
      <H>127</H> 
      <StickoutLength>101.6</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>76.2</FluteLength> 
      <ShankDiameter>19.05</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="65" ParentID="" Type="BALL END" PolishType="Polisher"> 
      <D>20</D> 
      <R>10</R> 
      <E>0</E> 
      <F>10</F> 
      <A>0</A> 
      <B>0</B> 
      <H>127</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>76.2</FluteLength> 
      <ShankDiameter>19.05</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="42" ParentID="" Type="BULL NOSE" PolishType="Polisher"> 
      <D>10</D> 
      <R>2</R> 
      <E>3</E> 
      <F>2</F> 
      <A>0</A> 
      <B>0</B> 
      <H>127</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>76.2</FluteLength> 
      <ShankDiameter>19.05</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-114.3</Z> 
     </DrivenPoint> 
    </Tool> 
    </Tools> 

這是排序後的預期XML內容:

<?xml version="1.0"?> 
    <Tools> 
    <Tool ID="Parametric Mills" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Teeth>2</Teeth> 
     <Type>Milling</Type> 
     <Cutter> 
     <Apt ID="2" ParentID="" Type="BALL END" > 
      <D>12.7</D> 
      <R>6.35</R> 
      <E>0</E> 
      <F>6.35</F> 
      <A>0</A> 
      <B>0</B> 
      <H>88.9</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="33" ParentID="" Type="TAPERED BULL NOSE" > 
      <D>12.7</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>0</A> 
      <B>5</B> 
      <H>88.9</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <ShankDiameter>12.7</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="3000" ParentID="" Type="BULL NOSE" > 
      <D>12.7</D> 
      <R>2</R> 
      <E>4.35</E> 
      <F>2</F> 
      <A>0</A> 
      <B>0</B> 
      <H>88.9</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="10000" ParentID="" Type="FLAT END" > 
      <D>12.7</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>0</A> 
      <B>0</B> 
      <H>88.9</H> 
      <StickoutLength>63.5</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>50.8</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-114.3</Z> 
     </DrivenPoint> 
    </Tool> 
    <Tool ID="Parametric Drills" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Teeth>2</Teeth> 
     <Type>HoleMaking</Type> 
     <Cutter> 
     <CenterDrill ID="3" ParentID="" DRILLMILL="FALSE" Type="CenterDrill" > 
      <ChamferAngle>60</ChamferAngle> 
      <H>50</H> 
      <PilotAngle>31</PilotAngle> 
      <D>3</D> 
      <PilotLength>3</PilotLength> 
      <ShankDiameter>12</ShankDiameter> 
      <StickoutLength>0</StickoutLength> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>11.69552</FluteLength> 
      <Alternate>off</Alternate> 
     </CenterDrill> 
     <Reamer ID="111" ParentID="" DRILLMILL="FALSE" Type="Reamer" > 
      <ChamferLength>0.5</ChamferLength> 
      <D>12</D> 
      <H>100</H> 
      <ShankDiameter>12</ShankDiameter> 
      <StickoutLength>0</StickoutLength> 
      <SpindleDirection>CW</SpindleDirection> 
      <MAXIMUMMATERIALREMOVAL>0</MAXIMUMMATERIALREMOVAL> 
      <FluteLength>63.5</FluteLength> 
      <Alternate>off</Alternate> 
     </Reamer> 
     <Apt ID="5000" ParentID="" Type="DRILL" > 
      <D>9.525</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>31</A> 
      <B>0</B> 
      <H>101.6</H> 
      <StickoutLength>76.2</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>63.5</FluteLength> 
      <Alternate>off</Alternate> 
     </Apt> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-127</Z> 
     </DrivenPoint> 
    </Tool> 
    <Tool ID="Tool Holders" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Teeth>2</Teeth> 
     <Type>Turning</Type> 
     <Holder> 
     <Cylinder ID="1" ParentID=""> 
      <Radius>50</Radius> 
      <Height>10</Height> 
      <NoSpin>0</NoSpin> 
      <Alternate>off</Alternate> 
     </Cylinder> 
     <Block ID="455" ParentID=""> 
      <Width>25.4</Width> 
      <Length>25.4</Length> 
      <Height>101.6</Height> 
      <NoSpin>0</NoSpin> 
      <Alternate>off</Alternate> 
     </Block> 
     <Cone ID="50000" ParentID=""> 
      <ButtomRadius>30</ButtomRadius> 
      <TopRadius>10</TopRadius> 
      <Height>50</Height> 
      <NoSpin>0</NoSpin> 
      <Alternate>off</Alternate> 
     </Cone> 
     </Holder> 
    </Tool> 
    <Tool ID="Probe Stylus" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Type>Probing</Type> 
     <Cutter> 
     <Probe ID="10455" ParentID="" Type="Cylinder"> 
      <SphereDiameter>10</SphereDiameter> 
      <Height>10</Height> 
      <StemDiameter>3</StemDiameter> 
      <Length>50</Length> 
      <MaxRPM>0</MaxRPM> 
      <StemRGB>-4144960</StemRGB> 
      <StickoutLength>0</StickoutLength> 
      <Alternate>off</Alternate> 
     </Probe> 
     <Probe ID="11111" ParentID="" Type="Disk"> 
      <SphereDiameter>20</SphereDiameter> 
      <Height>3</Height> 
      <Height2>5</Height2> 
      <Diameter2>3</Diameter2> 
      <PinOffset>8</PinOffset> 
      <StemDiameter>6</StemDiameter> 
      <Length>50</Length> 
      <MaxRPM>0</MaxRPM> 
      <StemRGB>-4144960</StemRGB> 
      <StickoutLength>0</StickoutLength> 
      <Alternate>off</Alternate> 
     </Probe> 
     <Probe ID="11500" ParentID="" Type="Standard"> 
      <SphereDiameter>5.00126</SphereDiameter> 
      <Height>101.6</Height> 
      <StemDiameter>2.99974</StemDiameter> 
      <Length>101.6</Length> 
      <MaxRPM>0</MaxRPM> 
      <StemRGB>-4144960</StemRGB> 
      <StickoutLength>76.2</StickoutLength> 
      <Alternate>off</Alternate> 
     </Probe> 
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-127</Z> 
     </DrivenPoint> 
    </Tool> 
    <Tool ID="Polishers" Units="Millimeter" Stack="FALSE"> 
     <Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0" 
    feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000" 
      force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000" 
      force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/> 
     <Type>Polisher</Type> 
     <Cutter> 
     <Apt ID="42" ParentID="" Type="BULL NOSE" PolishType="Polisher"> 
      <D>10</D> 
      <R>2</R> 
      <E>3</E> 
      <F>2</F> 
      <A>0</A> 
      <B>0</B> 
      <H>127</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>76.2</FluteLength> 
      <ShankDiameter>19.05</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="50" ParentID="" Type="FLAT END" PolishType="Polisher"> 
      <D>38.1</D> 
      <R>0</R> 
      <E>0</E> 
      <F>0</F> 
      <A>0</A> 
      <B>0</B> 
      <H>127</H> 
      <StickoutLength>101.6</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>76.2</FluteLength> 
      <ShankDiameter>19.05</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt> 
     <Apt ID="65" ParentID="" Type="BALL END" PolishType="Polisher"> 
      <D>20</D> 
      <R>10</R> 
      <E>0</E> 
      <F>10</F> 
      <A>0</A> 
      <B>0</B> 
      <H>127</H> 
      <StickoutLength>0</StickoutLength> 
      <R2>0</R2> 
      <E2>0</E2> 
      <F2>0</F2> 
      <SpindleDirection>CW</SpindleDirection> 
      <FluteLength>76.2</FluteLength> 
      <ShankDiameter>19.05</ShankDiameter> 
      <Alternate>off</Alternate> 
     </Apt>   
     </Cutter> 
     <DrivenPoint ID="1"> 
     <Type>-1</Type> 
     <Radius>0</Radius> 
     <X>0</X> 
     <Y>0</Y> 
     <Z>-114.3</Z> 
     </DrivenPoint> 
    </Tool> 
    </Tools> 

這是我正在使用的代碼,但輸出文件是bei ng作爲單行返回 XML文件...我有點迷失了關於如何在標籤「Apt」,「CenterDrill」,「Reamer」,「Cylinder」之後對包含ID屬性的節點進行排序。 「塊」,「型號」,「流水不腐」,「錐」,「探頭」和其他幾個人而不包含由「工具」「DrivenPoint」標籤之前的ID屬性節點搞亂/節點。

我對這個解決方案進行了很多研究和搜索,但它們似乎都基於更簡單的XML結構......我不介意爲每種節點類型編寫代碼,下面在我的代碼中嘗試做一些與「探針」節點,但它不工作或者...我得到一個1kb的輸出文件,其中只有一行...

有什麼建議嗎?

Imports System 
Imports System.Collections.Generic 
Imports System.Linq 
Imports System.Xml 
Imports System.Xml.Linq 

Public Class Form1 

    Private Function ReorderByMaterialThickness(ByRef inFile As String, ByRef outFile As String) 

     Try 
      Dim document As XElement = XElement.Load(inFile) 

      Dim sortedTables As XElement() = document.Elements("Probe").OrderBy(Function(t) CInt(t.Element("ID"))).ToArray() 
      document.ReplaceAll(sortedTables) 
      document.Save(outFile) 
     Catch ex As Exception 
      MsgBox("Something went wrong!") 
     End Try 

     Return Nothing 
    End Function 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     ReorderByMaterialThickness("C:\Testes\Original.xml", "C:\Testes\Sorted.xml") 
    End Sub 
End Class 
+2

如何使用專用於XML的技術?理想情況下,XSLT。 –

+0

如果有人能指出我正確的方向,我很樂意學習它。目前我無法知道如何使用它... –

+1

您可以嘗試將此問題變成XSLT問題,而不是使用'xslt'來標記它。但之前:確保在這裏包含一個最小的,完整的輸入和輸出文檔樣本,而不是鏈接和許多代碼行。幫助:http://stackoverflow.com/help/mcve。請記住:我不知道vb.net中是否有簡單的解決方案 - 可能會有一個。 –

回答

2

好吧,評論有很多,但我沒有注意到,直到我回來,但我相信這是做你想要的。它只對具有2個或更多祖先的元素進行排序,所以我相信它只是針對帶有ID的「內部」元素並對它們進行排序。當我用你的原始XML進行試用時,我得到了你說的你想要的輸出。

XDocument doc = XDocument.Load("Original.xml"); 
    foreach (XElement el in doc.Root.Descendants().Where(x => x.Elements().Any(y => y.Attribute("ID") != null) && x.Ancestors().Count() >= 2)) 
    { 
     el.ReplaceNodes(el.Elements().OrderBy(x => int.Parse(x.Attribute("ID").Value))); 
    } 
    doc.Save("newFile.xml"); 

我對你的文章有點困惑,但我認爲這是你要求的。

如果你不想使用的XDocument:

XElement doc = XElement.Load("Original.xml"); 
    foreach (XElement el in doc.Descendants().Where(x => x.Elements().Any(y => y.Attribute("ID") != null) && x.Ancestors().Count() >= 2)) 
    { 
     el.ReplaceNodes(el.Elements().OrderBy(x => int.Parse(x.Attribute("ID").Value))); 
    } 
    doc.Save("newFile.xml"); 
+0

它幾乎奏效!但是,你的代碼搞亂了節點

+0

你是什麼意思? – golgothan3

+0

對不起,我認爲原始xml會在多行中打破節點,因爲它非常長,當我們用代碼輸出它時,它會寫入一行。我犯了一個錯誤,這並不是說你的代碼搞亂了源代碼,它只是把它寫在一行中。我的錯。 –

2

AFAICT,應用以下XSL轉換到XML文件會產生所需的結果:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Cutter | Holder"> 
    <xsl:copy> 
     <xsl:apply-templates> 
      <xsl:sort select="@ID" data-type="number" order="ascending"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

演示:http://xsltransform.net/ncdD7mT

+1

哦,這篇文章是如此的豐富!我會在xslt中嘗試一下......我認爲它對我來說很重要,以便學習它! –

+0

邁克爾,我在你分享的模擬器中試過......看起來很完美!我必須學習xls! –