选择特定的子节点/元素

时间:2013-10-28 14:18:19

标签: c# xml

这个让我感到困惑。我可以在node.ChildNodes上做一个foreach循环,我可以看到一个节点的值为{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);

2 个答案:

答案 0 :(得分: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);

编辑: 完成后,这是代码正在处理的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>

答案 1 :(得分: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();