0

This one is confusing to me. I can do a foreach loop on node.ChildNodes and I can see that one node has the value of {Element, Name="DocId"}

but when i try to select XmlNodeList tableCells = node.SelectNodes("./DocId") or XmlNodeList tableCells = node.SelectNodes(".//DocId") or XmlNodeList tableCells = node.SelectNodes("DocId") it always returns null. I don't get it.

Here is the 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>

real answer

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

I always hear someone saying "Hey Mike, do not worry about it, if it is frustrating you, let it go and do your daily meditation".

Just kidding! Maybe you should try to use a nampespace manager. Little code excerpt from my own project.

        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: And to be complete, this was the XML file the code is working on.

<?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 类型到另一种的转换。我更喜欢 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 回答