0

これは私を混乱させます。node.ChildNodes で foreach ループを実行すると、1 つのノードの値が{Element, Name="DocId"}

XmlNodeList tableCells = node.SelectNodes("./DocId")しかし、または選択しようとする とXmlNodeList tableCells = node.SelectNodes(".//DocId")XmlNodeList tableCells = node.SelectNodes("DocId")常にnullが返されます。理解できません。

ここにXMLがあります

<SOP LogId="5237" LastModified="2013-10-21T14:10:49" xmlns="com/">
  <TargetInfo>
  </TargetInfo>
  <LawsuitInfo>
  </LawsuitInfo>
  <CaseType>Standard</CaseType>
  <Remark />
  <CourtInfo>
  </CourtInfo>
  <AttorneyInfo>
  </AttorneyInfo>
  <AgencyInfo>
  </AgencyInfo>
  <CaseInfo>
  </CaseInfo>
  <DocId>965F53E3C702</DocId>
  <DocketHistory>
  </DocketHistory>
</SOP>

本当の答え

XmlNode node = Detail(logId);
XmlDocument xDoc = node.OwnerDocument;
XmlNamespaceManager xmlnsm = new XmlNamespaceManager(xDoc.NameTable);
xmlnsm.AddNamespace("x", "http://usop.ctadvantage.com/");
XmlNodeList tableCells = node.SelectNodes("/x:DocId", xmlnsm);
4

2 に答える 2

0

私はいつも誰かが「ヘイ、マイク、気にしないで、イライラするなら手放して、毎日の瞑想をしなさい」と言っているのを耳にします。

冗談だ!nampespace マネージャーを使用してみてください。私自身のプロジェクトからのコードの抜粋。

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(ConfigFile);
        XmlNamespaceManager xmlnsm = new XmlNamespaceManager(xmlDoc.NameTable);
        xmlnsm.AddNamespace("h", "urn:nhibernate-configuration-2.2");

        XmlElement root = xmlDoc.DocumentElement;
        //Do you XML magic here with namespace included
        XmlNodeList Properties = root.SelectNodes("h:session-factory/h:property[@name='connection.connection_string']", xmlnsm);

EDIT:そして、これはコードが取り組んでいるXMLファイルでした。

<?xml version="1.0" encoding="utf-8"?>
<!-- 
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
for your own use before compile tests in VisualStudio.
-->
<!-- This is the ByteFX.Data.dll provider for MySql -->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="NHibernate.Test">
        <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
        <property name="connection.connection_string">
      Data Source=127.0.0.1;Port=3307;MyUser Id=root;Password=MyPassword;Database=MyDatabase;
    </property>
        <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>
于 2013-10-28T14:24:56.700 に答える
0

このようなクラスを使用して、ある XML タイプから別の XML タイプへの変換を行うことができます。私は Linq-to-XML を好むので、次のことを示します。

public static class XmlLinqConversionExtensions
{
    /// <summary>
    /// Converts an XDocument to an XmlDocument.
    /// </summary>
    /// <param name="xdoc">The XDocument to convert.</param>
    /// <returns>The equivalent XmlDocument.</returns>
    public static XmlDocument ToXmlDocument(this XDocument xdoc)
    {
        var xmldoc = new XmlDocument();
        xmldoc.Load(xdoc.CreateReader());
        return xmldoc;
    }

    /// <summary>
    /// Convert an XElement to an XmlDocument.
    /// </summary>
    /// <param name="xelement"></param>
    /// <returns></returns>
    public static XmlDocument ToXmlDocument(this XElement xelement)
    {
        var xmldoc = new XmlDocument();
        xmldoc.Load(xelement.CreateReader());
        return xmldoc;
    }

    /// <summary>
    /// Converts an XmlDocument to an XDocument.
    /// </summary>
    /// <param name="xmldoc">The XmlDocument to convert.</param>
    /// <returns>The equivalent XDocument.</returns>
    public static XDocument ToXDocument(this XmlDocument xmldoc)
    {
        return XDocument.Load(xmldoc.CreateNavigator().ReadSubtree());
    }

    /// <summary>
    /// Converts an XmlDocument to an XElement.
    /// </summary>
    /// <param name="xmldoc"></param>
    /// <returns></returns>
    public static XElement ToXElement(this XmlDocument xmldoc)
    {
        return ToXDocument(xmldoc).Root;
    }

    /// <summary>
    /// Converts an XElement to an XmlElement.
    /// </summary>
    /// <param name="xelement">The XElement to convert.</param>
    /// <returns>The equivalent XmlElement.</returns>
    public static XmlElement ToXmlElement(this XElement xelement)
    {
        return new XmlDocument().ReadNode(xelement.CreateReader()) as XmlElement;
    }

    /// <summary>
    /// Converts an XmlElement to an XElement.
    /// </summary>
    /// <param name="xmlelement">The XmlElement to convert.</param>
    /// <returns>The equivalent XElement.</returns>
    public static XElement ToXElement(this XmlElement xmlelement)
    {
        return XElement.Load(xmlelement.CreateNavigator().ReadSubtree());
    }
}

したがって、ノードを XElement に変換することから始めます。

XElement xnode = node.ToXElement();

次に、すべての DocId を選択します。

XNamespace ns = "http://usop.ctadvantage.com/";
var docIds = xnode.Descendants(ns + "DocId");

および/またはその Id 値で特定のノードを取得します。

XElement specificdocId = docIds.Where(x => x.Value == "965F53E3C702").FirstOrDefault();
于 2013-10-28T18:32:16.650 に答える