现在我有这样一个xml文件:<?xml version="1.0" encoding="UTF-8"?>
<!--    Example of Well data  -->
<wells 
xmlns="http://www.witsml.org/schemas/1series" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.witsml.org/schemas/1series  ../xsd_schemas/obj_well.xsd" 
version="1.4.0.0">
<!-- 
These examples are only intended to demonstrate the type of data that can be exchanged.
They totally artificial and are not intended to demonstrate best practices. 
-->
<documentInfo>
<documentName>well</documentName>
<fileCreationInformation>
<fileCreationDate>2001-10-31T08:15:00.000Z</fileCreationDate> 
<fileCreator>John Smith</fileCreator>
</fileCreationInformation>
</documentInfo>
<well uid="w-12">
<name>6507/7-A-42</name>
<nameLegal>Company Legal Name</nameLegal>
<numLicense>Company License Number</numLicense>
<numGovt>Govt-Number</numGovt>
<dTimLicense>2001-05-15T13:20:00.000Z</dTimLicense>
<field>Big Field</field>
<country>US</country>
<state>TX</state>
<county>Montgomery</county>
<region>Region Name</region>
<district>District Name</district>
<block>Block Name</block>
<timeZone>-06:00</timeZone>
<operator>Operating Company</operator>
<operatorDiv>Division Name</operatorDiv>
<pcInterest uom="%">65</pcInterest>
<numAPI>123-543-987AZ</numAPI>
<statusWell>drilling</statusWell>
<purposeWell>exploration</purposeWell>
<dTimSpud>2001-05-31T08:15:00.000Z</dTimSpud>
<dTimPa>2001-07-15T15:30:00.000Z</dTimPa>
<wellheadElevation uom="ft">500</wellheadElevation>
<wellDatum uid="KB" defaultMeasuredDepth="true" defaultVerticalDepth="true">
<name>Kelly Bushing</name>
<code>KB</code>
<elevation uom="ft" datum="SL">78.5</elevation>
</wellDatum>
<wellDatum uid="SL" defaultElevation="true">
<name>Sea Level</name>
<code>SL</code>
</wellDatum>
<groundElevation uom="ft">250</groundElevation>
<waterDepth uom="ft">520</waterDepth>
<wellLocation uid="loc-1">
<wellCRS uidRef="proj1">ED50 / UTM Zone 31N</wellCRS>
<easting uom="m">425353.84</easting>
<northing uom="m">6623785.69</northing>
<description>Location of well surface point in projected system.</description>
</wellLocation>
<referencePoint uid="SRP1">
<name>Slot Bay Centre</name>
<type>Site Reference Point</type>
<location uid="loc-1">
<wellCRS uidRef="proj1">ED50 / UTM Zone 31N</wellCRS>
<easting uom="m">425366.47</easting>
<northing uom="m">6623781.95</northing>
</location>
<location uid="loc-2">
<wellCRS uidRef="localWell1">WellOneWSP</wellCRS>
<localX uom="m">12.63</localX>
<localY uom="m">-3.74</localY>
<description>Location of the Site Reference Point with respect to the well surface point</description>
</location>
</referencePoint>
<referencePoint uid="WRP2">
<name>Sea Bed</name>
<type>Well Reference Point</type>
<elevation uom="ft" datum="SL">-118.40</elevation>
<measuredDepth uom="ft" datum="KB">173.09</measuredDepth>
<location uid="loc-1">
<wellCRS uidRef="proj1">ED50 / UTM Zone 31N</wellCRS>
<easting uom="m">425353.84</easting>
<northing uom="m">6623785.69</northing>
</location>
<location uid="loc-2">
<wellCRS uidRef="geog1">ED50</wellCRS>
<latitude uom="dega">59.743844</latitude>
<longitude uom="dega">1.67198083</longitude>
</location>
</referencePoint>
<wellCRS uid="geog1">
<name>ED50</name>
     <geodeticCRS uidRef="4230">4230</geodeticCRS >
     <description>ED50 system with EPSG code 4230.</description>
</wellCRS>
<wellCRS uid="proj1">
<name>ED50 / UTM Zone 31N</name>
<mapProjectionCRS uidRef="23031">ED50 / UTM Zone 31N</mapProjectionCRS>
</wellCRS>
<wellCRS uid="localWell1">
<name>WellOneWSP</name>
<localCRS>
<usesWellAsOrigin>true</usesWellAsOrigin>
<yAxisAzimuth uom="dega" northDirection="grid north">0</yAxisAzimuth>
<xRotationCounterClockwise>false</xRotationCounterClockwise>
</localCRS>
</wellCRS>
<commonData>
<dTimCreation>2001-04-30T08:15:00.000Z</dTimCreation>
<dTimLastChange>2001-05-31T08:15:00.000Z</dTimLastChange>
<itemState>plan</itemState>
<comments>These are the comments associated with the Well data object.</comments>
</commonData>
</well>
<well uid="w-13">
<name>6507/7-A-42</name>
<nameLegal>Company Legal Name</nameLegal>
<numLicense>Company License Number</numLicense>
<numGovt>Govt-Number</numGovt>
<dTimLicense>2001-05-15T13:20:00.000Z</dTimLicense>
<field>Big Field</field>
<country>US</country>
<state>TX</state>
<county>Montgomery</county>
<region>Region Name</region>
<district>District Name</district>
<block>Block Name</block>
<timeZone>-06:00</timeZone>
<operator>Operating Company</operator>
<operatorDiv>Division Name</operatorDiv>
<pcInterest uom="%">65</pcInterest>
<numAPI>123-543-987AZ</numAPI>
<statusWell>drilling</statusWell>
<purposeWell>exploration</purposeWell>
<dTimSpud>2001-05-31T08:15:00.000Z</dTimSpud>
<dTimPa>2001-07-15T15:30:00.000Z</dTimPa>
<wellheadElevation uom="ft">500</wellheadElevation>
<wellDatum uid="KB" defaultMeasuredDepth="true" defaultVerticalDepth="true">
<name>Kelly Bushing</name>
<code>KB</code>
<elevation uom="ft" datum="SL">78.5</elevation>
</wellDatum>
<wellDatum uid="SL" defaultElevation="true">
<name>Sea Level</name>
<code>SL</code>
</wellDatum>
<groundElevation uom="ft">250</groundElevation>
<waterDepth uom="ft">520</waterDepth>
<wellLocation uid="loc-1">
<wellCRS uidRef="proj1">ED50 / UTM Zone 31N</wellCRS>
<easting uom="m">425353.84</easting>
<northing uom="m">6623785.69</northing>
<description>Location of well surface point in projected system.</description>
</wellLocation>
<referencePoint uid="SRP1">
<name>Slot Bay Centre</name>
<type>Site Reference Point</type>
<location uid="loc-1">
<wellCRS uidRef="proj1">ED50 / UTM Zone 31N</wellCRS>
<easting uom="m">425366.47</easting>
<northing uom="m">6623781.95</northing>
</location>
<location uid="loc-2">
<wellCRS uidRef="localWell1">WellOneWSP</wellCRS>
<localX uom="m">12.63</localX>
<localY uom="m">-3.74</localY>
<description>Location of the Site Reference Point with respect to the well surface point</description>
</location>
</referencePoint>
<referencePoint uid="WRP2">
<name>Sea Bed</name>
<type>Well Reference Point</type>
<elevation uom="ft" datum="SL">-118.40</elevation>
<measuredDepth uom="ft" datum="KB">173.09</measuredDepth>
<location uid="loc-1">
<wellCRS uidRef="proj1">ED50 / UTM Zone 31N</wellCRS>
<easting uom="m">425353.84</easting>
<northing uom="m">6623785.69</northing>
</location>
<location uid="loc-2">
<wellCRS uidRef="geog1">ED50</wellCRS>
<latitude uom="dega">59.743844</latitude>
<longitude uom="dega">1.67198083</longitude>
</location>
</referencePoint>
<wellCRS uid="geog1">
<name>ED50</name>
     <geodeticCRS uidRef="4230">4230</geodeticCRS >
     <description>ED50 system with EPSG code 4230.</description>
</wellCRS>
<wellCRS uid="proj1">
<name>ED50 / UTM Zone 31N</name>
<mapProjectionCRS uidRef="23031">ED50 / UTM Zone 31N</mapProjectionCRS>
</wellCRS>
<wellCRS uid="localWell1">
<name>WellOneWSP</name>
<localCRS>
<usesWellAsOrigin>true</usesWellAsOrigin>
<yAxisAzimuth uom="dega" northDirection="grid north">0</yAxisAzimuth>
<xRotationCounterClockwise>false</xRotationCounterClockwise>
</localCRS>
</wellCRS>
<commonData>
<dTimCreation>2001-04-30T08:15:00.000Z</dTimCreation>
<dTimLastChange>2001-05-31T08:15:00.000Z</dTimLastChange>
<itemState>plan</itemState>
<comments>These are the comments associated with the Well data object.</comments>
</commonData>
</well>
</wells>我想在其中查找所有well节点,我这样做没有没有得到任何结果: 
XmlNodeList nodeWell = wells.SelectNodes("//wells/well");
怎么回事?

解决方案 »

  1.   

    使用这个
     XmlDocument doc = new XmlDocument();
    doc.Load(xml路径)
     XmlNodeList nodeWell = doc.GetElementsByTagName("well")
      

  2.   

    GetElementsByTagName 返回一个匹配的结点集合
      

  3.   

    2楼的方法试过了,不行。3楼4楼的方法是可以的。奇怪的是为什么SelectNodes就不行呢?
      

  4.   

    XmlNodeList nodeWell = wells.SelectNodes("//well");这样就行了
      

  5.   

    很奇怪的是我对另外一个xml文档:<?xml version="1.0" encoding="UTF-8"?>
    <logs>
    <log uidWell="W-12" uidWellbore="B-0" uid="f34a">
    <nameWell>6507/7-A-42</nameWell>
    <nameWellbore>A-42</nameWellbore>
    <name>L001</name>
    </log>
    </logs>使用语句:
    XmlNode nodeUid = xIn.SelectSingleNode("//log");
    就可以查找的log节点,但是对那个内容较多的xml文档,SelectSingleNode和SelectNodes就不好用了
      

  6.   

    命名空间的问题,你的XML中无掉命名空间也是可用的。
      

  7.   

    主要是因为这个
    <wells 
        xmlns="http://www.witsml.org/schemas/1series" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.witsml.org/schemas/1series  ../xsd_schemas/obj_well.xsd" 
        version="1.4.0.0">
    多了 xmlns="http://www.witsml.org/schemas/1series" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.witsml.org/schemas/1series  ../xsd_schemas/obj_well.xsd" 
        version="1.4.0.0">
    如果没有这些,就可以了
      

  8.   

    XmlDocument doc = new XmlDocument();
    doc.Load("xml路径");
    XmlNodeList wells = doc.SelectNodes("wells/well");
      

  9.   

       System.Xml.XmlDocument _Document = new System.Xml.XmlDocument();
                _Document.Load(@"D:\temp\file1.xml");            System.Xml.XmlNamespaceManager _NamespaceManager = new System.Xml.XmlNamespaceManager(_Document.NameTable);
                _NamespaceManager.AddNamespace("Name", _Document.DocumentElement.NamespaceURI);            System.Xml.XmlNodeList _List = _Document.SelectNodes("//Name:well", _NamespaceManager);
      

  10.   

    public static void Main(string[] args)
            {
                string str = null;
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load("1.xml");
                XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDocument.NameTable);
                xnm.AddNamespace("XNM", "http://www.witsml.org/schemas/1series");
                XmlNodeList mynodelist = xmlDocument.SelectNodes("/XNM:wells/XNM:well", xnm);
                int index;
                for (index = 0; index < mynodelist.Count; index++)
                    {
                        XmlElement root = xmlDocument.DocumentElement;
                        if (mynodelist.Count.ToString() != null)
                        {
                           str+= mynodelist.Item(index).InnerText;
                        }                }
                Console.WriteLine(str);
                Console.ReadKey();        }
      

  11.   

    明白了,多谢zgke。现在碰到个问题我想查找uid属性为“W-12”的well节点,但是这个“W-12”是存储在字符串变量中的,有办法使用SelectSingalNode直接查找么?xpath都是字符串,感觉好像不行,不过还是问问
      

  12.   

     XmlNodeList nodelist = doc.SelectNodes("wells/well[@uid='w-12']");
      

  13.   


    XmlNode nodelist = doc.SelectSingleNode("wells/well[@uid='w-12']");这个是没有命名空间的情况,如果有命名空间的话,你可以按照zgke的尝试一下这样加一下
      

  14.   

     public static void Main(string[] args)
            {
                string str = null;
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load("1.xml");
                XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDocument.NameTable);
                xnm.AddNamespace("XNM", "http://www.witsml.org/schemas/1series");
                XmlNodeList mynodelist = xmlDocument.SelectNodes("/XNM:wells/XNM:well[@uid=\"w-12\"]", xnm);
                int index;
                for (index = 0; index < mynodelist.Count; index++)
                    {
                        XmlElement root = xmlDocument.DocumentElement;
                        if (mynodelist.Count.ToString() != null)
                        {
                           str+= mynodelist.Item(index).InnerText;
                        }                }
                Console.WriteLine(str);
                Console.ReadKey();        }
      

  15.   

    楼上2位没有明白我的意思,我是说“w-12”是存储在字符串变量中的,因为也可能是其他值。所以不能直接放在xpath里面啊
      

  16.   

    那可以使用拼接字符串
    ode nodelist = doc.SelectSingleNode("wells/well[@uid='”+变量+“']");
      

  17.   

    能把.NET 平台下的XML的操作学好  .NET就学好了 百分之30了吧