如何在VB .NET中升级LandXML节点

时间:2015-02-17 18:27:11

标签: xml vb.net

给出这样的XML结构:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <LandXML xmlns="http://www.landxml.org/schema/LandXML-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" date="2014-12-18-06:00" language="English" readOnly="false" time="12:12:16.338-06:00" version="1.0" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.0 http://www.landxml.org/schema/LandXML-1.0/LandXML-1.0.xsd">
        <Units>
                    <Imperial angularUnit="decimal degrees" areaUnit="squareFoot" diameterUnit="USSurveyFoot" directionUnit="decimal degrees" flowUnit="cubicFeetSecond" heightUnit="USSurveyFoot" linearUnit="USSurveyFoot" pressureUnit="inHG" temperatureUnit="fahrenheit" velocityUnit="feetPerSecond" volumeUnit="cubicFeet" widthUnit="USSurveyFoot" />
        </Units>
        <Surfaces>
            <Surface OID="SIE" desc="Ground Surface" name="Ground">
              <Definition surfType="TIN">
                <Pnts />
                <Faces />
              </Definition>
              <SourceData>
                <Boundaries>
                  <Boundary bndType="outer" edgeTrim="true" name="Outermost bounding edge">
                    <PntList3D>1.5181307542E7 2125297.27 1713.976 1.5181309121E7 2125348.661 1713.65 1.518131357E7 2125401.188 1713.624 1.5181355394E7 2125953.634 1712.622 1.5181471897E7 2127035.359 1711.076 1.5181474238E7 2127060.368 1711.571 1.5181479644E7 2127074.35 1711.4 1.5181484532E7 2127081.58 1711.23 1.5181486144E7 2127083.79 1711.24 1.5181526576E7 2127114.416 1710.763 1.5181604217E7 2127162.511 1710.742 1.518173257E7 2127220.112 1712.713 1.5182046629E7 2127316.035 1716.328 1.5182350908E7 2127362.323 1709.085 1.518238492E7 2126720.863 1678.373 1.5182383044E7 2126559.786 1663.524 1.518236883E7 2126031.128 1708.884 1.5182353785E7 2125775.21 1680.648 1.5182361822E7 2125348.267 1648.468 1.518235892E7 2125223.263 1646.325 1.518233926E7 2124751.224 1640.587 1.5181441405E7 2125081.466 1700.044 1.518132618E7 2125144.398 1708.546 1.5181317827E7 2125194.92 1712.947 1.5181309366E7 2125246.027 1714.161</PntList3D>
                  </Boundary>
                </Boundaries>
              </SourceData>
              <Definition surfType="GND">
                  <Pnts />
                  <Faces />
              </Definition>
    <SourceData>
                <Boundaries>
                  <Boundary bndType="outer" edgeTrim="true" name="Outermost bounding edge">
                    <PntList3D></PntList3D>
                  </Boundary>
                </Boundaries>
              </SourceData> 


              <Project desc="GPS Survey, UTM14 North, Autonomous Base, Geoid 2009, Reedy" />
              <Application manufacturer="USDA NRCS" name="Engineering Field Tools" version="3.4.1.2">
                <Author createdBy="james.reedy" />
              </Application>
            </Surface>
          </Surfaces>
        </LandXML>

我想在Surface子类别下移动第一个SourceData节点,但是在Surface子类别中的Definition上面。

这是我想要的最终结构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <LandXML xmlns="http://www.landxml.org/schema/LandXML-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" date="2014-12-18-06:00" language="English" readOnly="false" time="12:12:16.338-06:00" version="1.0" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.0 http://www.landxml.org/schema/LandXML-1.0/LandXML-1.0.xsd">
      <Units>
        <Imperial angularUnit="decimal degrees" areaUnit="squareFoot" diameterUnit="USSurveyFoot" directionUnit="decimal degrees" flowUnit="cubicFeetSecond" heightUnit="USSurveyFoot" linearUnit="USSurveyFoot" pressureUnit="inHG" temperatureUnit="fahrenheit" velocityUnit="feetPerSecond" volumeUnit="cubicFeet" widthUnit="USSurveyFoot" />
      </Units>
      <Surfaces>
        <Surface OID="SIE" desc="Ground Surface" name="Ground">
          <SourceData>
            <Boundaries>
              <Boundary bndType="outer" edgeTrim="true" name="Outermost bounding edge">
                <PntList3D>1.5181307542E7 2125297.27 1713.976 1.5181309121E7 2125348.661 1713.65 1.518131357E7 2125401.188 1713.624 1.5181355394E7 2125953.634 1712.622 1.5181471897E7 2127035.359 1711.076 1.5181474238E7 2127060.368 1711.571 1.5181479644E7 2127074.35 1711.4 1.5181484532E7 2127081.58 1711.23 1.5181486144E7 2127083.79 1711.24 1.5181526576E7 2127114.416 1710.763 1.5181604217E7 2127162.511 1710.742 1.518173257E7 2127220.112 1712.713 1.5182046629E7 2127316.035 1716.328 1.5182350908E7 2127362.323 1709.085 1.518238492E7 2126720.863 1678.373 1.5182383044E7 2126559.786 1663.524 1.518236883E7 2126031.128 1708.884 1.5182353785E7 2125775.21 1680.648 1.5182361822E7 2125348.267 1648.468 1.518235892E7 2125223.263 1646.325 1.518233926E7 2124751.224 1640.587 1.5181441405E7 2125081.466 1700.044 1.518132618E7 2125144.398 1708.546 1.5181317827E7 2125194.92 1712.947 1.5181309366E7 2125246.027 1714.161</PntList3D>
              </Boundary>
            </Boundaries>
          </SourceData>
          <Definition surfType="TIN">
            <Pnts />
            <Faces />
          </Definition>
<Definition surfType="GND">
            <Pnts />
            <Faces />
          </Definition>
<SourceData>
            <Boundaries>
              <Boundary bndType="outer" edgeTrim="true" name="Outermost bounding edge">
                <PntList3D></PntList3D>
              </Boundary>
            </Boundaries>
          </SourceData>
          <Project desc="GPS Survey, UTM14 North, Autonomous Base, Geoid 2009, Reedy" />
          <Application manufacturer="USDA NRCS" name="Engineering Field Tools" version="3.4.1.2">
            <Author createdBy="james.reedy" />
          </Application>
        </Surface>
      </Surfaces>
    </LandXML>

到目前为止,我已经提出了这个代码:

Sub MoveUnits(fileName As String)
    Dim file As XDocument = XDocument.Load(fileName)
    Dim SourceData As XName = XName.Get("{http://www.landxml.org/schema/LandXML-1.0}SourceData")
    Dim tempElement = file.Root.Elements(SourceData)(0)
    tempElement.Remove()
    'Dont know how to add it in the right spot
    file.Save(fileName)
End Sub

然而,它只会在tempElement.Remove()处输出一个错误,说它无法删除一个等于零的对象。所以我想:

Dim SourceData As XName = XName.Get("{http://www.landxml.org/schema/LandXML-1.0}SourceData")
Dim tempElement = file.Root.Elements(SourceData)(0)

因为它会在LandXML下找到任何类别而不是任何子类别。 请帮忙

1 个答案:

答案 0 :(得分:0)

您需要深入查看相应的元素:

Sub MoveSourceData(fileName As String)
    Dim file As XDocument = XDocument.Load(fileName)
    Dim surfaces As XName = XName.Get("{http://www.landxml.org/schema/LandXML-1.0}Surfaces")
    Dim surface As XName = XName.Get("{http://www.landxml.org/schema/LandXML-1.0}Surface")
    Dim sourcedata As XName = XName.Get("{http://www.landxml.org/schema/LandXML-1.0}SourceData")
    Dim rootElement = file.Root.Elements(surfaces)(0).Elements(surface)(0)
    Dim tempElement = rootElement.Elements(sourcedata)(0)
    tempElement.Remove()
    rootElement.AddFirst(tempElement)
    file.Save(fileName)
End Sub

如果您需要更改第一个元素以外的其他元素,只需更改索引即可。例如,第二个Surface中的第一个SourceData是:

Dim rootElement = file.Root.Elements(surfaces)(0).Elements(surface)(1)
Dim tempElement = rootElement.Elements(sourcedata)(0)

如果您只需要第一个元素,则可以使用Element属性,这样可以通过仅查找第一个元素而不是该名称的所有元素来提高大文件的性能:

Dim rootElement = file.Root.Element(surfaces).Element(surface)
Dim tempElement = rootElement.Element(sourcedata)