11

これは難しいことではないようですが、私は現在立ち往生しています。特定のXPathクエリ文字列に一致するノードから特定の属性の属性値を取得しようとしています。これが私がこれまでに持っているものです:

    public static IEnumerable<string> GetAttributes(this XmlDocument xml,
        string xpathQuery, string attributeName)
    {
        var doc = new XPathDocument(new XmlNodeReader(xml));
        XPathNavigator nav = doc.CreateNavigator();
        XPathExpression expr = nav.Compile(xpathQuery);
        XPathNodeIterator iterator = nav.Select(expr);
        while (iterator.MoveNext())
        {
            XPathNavigator curNav = iterator.Current;
            if (curNav.HasAttributes)
            {
                XmlNode curNode = ((IHasXmlNode)curNav).GetNode();
                if (null != curNode)
                {
                    XmlAttribute attrib = curNode.Attributes[attributeName];
                    if (null != attrib)
                    {
                        yield return attrib.Value;
                    }
                }
            }
        }
    }

これは現在例外をスローします:

System.InvalidCastException:タイプ「MS.Internal.Xml.Cache.XPathDocumentNavigator」のオブジェクトをタイプ「System.Xml.IHasXmlNode」にキャストできません。

私はこれについて間違っていますか?一致するノードから属性値を取得する簡単な方法はありますか?

4

3 に答える 3

33

次の xml の場合:

<root>
  <elem att='the value' />
</root>

このC#コードで「値」テキストを取得できます

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(text);
    Console.WriteLine(xdoc.SelectSingleNode("/root/elem/@att").Value);
于 2010-11-29T21:20:01.017 に答える
5

.net 3.5 以降を使用している場合は、linq to Xml を使用できます

特定の xml ドキュメントについて

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <storedProcedures>
    <storedProcedure name="usp_GET_HOME_PAGE_DATA">
      <resultSet name="Features"/>
      <resultSet name="Highlights"/>
    </storedProcedure>
    <storedProcedure name="usp_GET_FEATURES" />
    <storedProcedure name="usp_GET_FEATURE" />
    <storedProcedure name="usp_UPDATE_FEATURE" />
    <storedProcedure name="usp_GET_FEATURE_FOR_DISPLAY">
      <resultSet name="CurrentFeature"/>
      <resultSet name="OtherFeatures"/>
    </storedProcedure>
    <storedProcedure name="usp_GET_HIGHLIGHT_TITLES">
      <resultSet name="Highlights"/>
    </storedProcedure>
  </storedProcedures>
</root>

次の linq 式は、すべての storedProcedure ノードの「name」属性の値を取得します。

XDocument xDcoument = XDocument.Load(xmlStoredProcSchemeFile);

  var storedProcedureNames = from doc in xDcoument.Descendants("storedProcedure")
                             select doc.Attribute("name").Value;

通常の XPath 構文を使用することもできます。以下のコードでは、変数 node は「usp_GET_HOME_PAGE_DATA」という名前で識別されるノードを保持し、属性変数は選択したノードのすべての子ノード (属性) とその子を保持します。

  XmlDocument xmlDocument = new XmlDocument();
  xmlDocument.Load(@"C:\inetpub\wwwroot\ASPNETBuilder\BusinessLayer\DataAccessCodeGenerationSchema.xml");
  var node = xmlDocument.DocumentElement.SelectSingleNode("./storedProcedures/storedProcedure[@name='usp_GET_HOME_PAGE_DATA']");
  var attributes = node.SelectNodes("./resultSet/@name");
于 2010-11-29T21:25:36.893 に答える
1

例外がスローされるという最初の問題の解決策...

var doc = new XPathDocument(new XmlNodeReader(xml));

に置き換える必要があります...

var doc = new XmlDocument();
doc.load(*you can either specify the path to the file, the string out of which the xml document is to be generated or specify an xmlreader, look for more overloads*);

これは例外をスローせず、コードは問題なく動作します。

于 2011-01-17T07:40:40.593 に答える