给出这样的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">
<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>
<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>
<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>
我想将<Units>
节点移到<LandXML>
下方的顶部,因此它显示在表面节点节点上方,我需要将<SourceData>
节点移到<Surface>
下面}。
这是我想要的最终结构:
<?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>
<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>
到目前为止,我已经提出了这个代码:
Dim doc As XmlDocument = New XmlDocument()
doc.Load(Xml_in)
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("", "http://www.landxml.org/schema/LandXML-1.0")
Dim nodelist As XmlNodeList = doc.SelectNodes("//LandXML", nsmgr)
Dim nodelist1 As XmlNode = doc.SelectSingleNode("//LandXML")
For i As Integer = 0 To nodelist.Count - 1
If (nodelist(i).HasChildNodes) Then
Dim nodes As XmlNodeList = nodelist(i).ChildNodes
For j As Integer = nodelist(i).ChildNodes.Count - 1 To 0 Step -1
If (nodes(j).Name = "Units") Then
msgbox("Found Units")
Dim xn As XmlNode = nodes(j)
nodelist(i).RemoveChild(nodes(j))
nodelist1.InsertBefore(xn, nodelist1.FirstChild)
End If
Next
End If
Next
doc.Save(Xml_out)
MsgBox("Done")
但是它只会输出原始文件,并且永远不会显示第一个消息框。我认为它没有找到<Units>
,我无法弄清楚如何得到它。
答案 0 :(得分:1)
我不认为你的用例应该与子节点的顺序有关。
它在语义上是相同的:在<SourceData>
下面只有一个 <Surface>
子项,无论它是第一个孩子还是最后一个孩子都没关系。你可以简单地通过名字来引用它。
如果您目前正在构建依赖<SourceData>
成为第一个孩子的东西,我强烈建议您重新考虑。
XmlNamespaceManager
(*) Shared Sub ProcessXml(Xml_in As String, Xml_out As String)
Dim doc = New XmlDocument()
doc.Load(Xml_in)
Dim nsmgr = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("land", "http://www.landxml.org/schema/LandXML-1.0")
SetAsFirstChild(doc.SelectSingleNode("//land:Units", nsmgr), doc.DocumentElement)
SetAsFirstChild(doc.SelectSingleNode("//land:SourceData", nsmgr))
doc.Save(Xml_out)
End Sub
Shared Sub SetAsFirstChild(node As XmlNode, Optional parent As XmlNode = Nothing)
If node Is Nothing Then Exit Sub
If parent Is Nothing Then parent = node.ParentNode
If parent Is Nothing Then Exit Sub
parent.InsertBefore(node, parent.FirstChild)
End Sub
(*)从技术上讲,你可以。但是,这不会影响XPath。请参阅the docs。
答案 1 :(得分:1)
鉴于你原来不完整的xml代码,我认为,根据缩进,你的意思是这样的:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<LandXML 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" xmlns="http://www.landxml.org/schema/LandXML-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Surfaces>
<Surface OID="SIE" desc="Ground Surface" name="Ground">
<Definition surfType="TIN">
<Pnts></Pnts>
<Faces></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>
</Surface>
</Surfaces>
<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>
<Project desc="GPS Survey, UTM14 North, Autonomous Base, Geoid 2009, Reedy"></Project>
<Application manufacturer="USDA NRCS" name="Engineering Field Tools" version="3.4.1.2">
<Author createdBy="james.reedy" />
</Application>
</LandXML>
使用XDocument这样的东西可以工作:
Sub MoveUnits(fileName As String)
Dim file As XDocument = XDocument.Load(fileName)
Dim units As XName = XName.Get("{http://www.landxml.org/schema/LandXML-1.0}Units")
Dim tempElement = file.Root.Elements(units)(0)
tempElement.Remove()
file.Root.AddFirst(tempElement)
file.Save(fileName)
End Sub
对于您想要移动的其他元素,应该很容易遵循相同的步骤。 这将导致以下结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<LandXML 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" xmlns="http://www.landxml.org/schema/LandXML-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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></Pnts>
<Faces></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>
</Surface>
</Surfaces>
<Project desc="GPS Survey, UTM14 North, Autonomous Base, Geoid 2009, Reedy"></Project>
<Application manufacturer="USDA NRCS" name="Engineering Field Tools" version="3.4.1.2">
<Author createdBy="james.reedy" />
</Application>
</LandXML>