Xmldoc .selectsinglenode返回null

时间:2013-09-25 17:57:25

标签: c# xml

我真的只是想选择

中列出的值
    SOPDetail  
    TargetInfo   
DomesticJurisidiction

每个都是另一个的子节点。当我进行迭代时,我找到了节点DomesticJurisidiction,但是当我选择使用xpath时,我无法找到它。有没有人建议为什么?

        /*XmlNode targetNode = xDoc.SelectSingleNode("/SOPDetail/TargetInfo/DomesticJurisidiction");
        string haha = targetNode.InnerXml;*/

        foreach (XmlNode xmlNode in xDoc.ChildNodes)
        {
            Console.WriteLine(xmlNode.Name);
            foreach (XmlNode chldNode in xmlNode.ChildNodes)
            {
                Console.WriteLine(chldNode.Name);
                foreach (XmlNode cNode in chldNode.ChildNodes)
                {
                    Console.WriteLine(cNode.Name + ":" + cNode.InnerXml);
                }
            }
        }

就像我提到的foreach迭代完美地找到了这个节点,为什么我不能用SelectsingleNode选择它?

<SOPDetail LogId="324" LastModified="2007-05-20T09:20:50" xmlns="http://usop.ctadvantage.com/">
  <TargetInfo>
    <DomesticJurisdiction>Connecticut</DomesticJurisdiction>
    <ServedName>CT Demo Account (All State Corp)</ServedName>
    <ServedJurisdiction>New York</ServedJurisdiction>
    <NameDiscrepancyExists>false</NameDiscrepancyExists>
    <Defendant>Test</Defendant>
  </TargetInfo>
  <LawsuitInfo>
    <ReceiptDate>2007-05-20</ReceiptDate>
    <CreatedDate>2007-05-20T08:41:38</CreatedDate>
    <MethodOfService>Courier</MethodOfService>
    <LawsuitType>Other</LawsuitType>
    <LawsuitSubType>Documents requiring hard copy delivery</LawsuitSubType>
    <AnswerDate />
    <DocumentServed>Acknowledgment of Service</DocumentServed>
    <NatureOfAction>
      Notifying of Dft. 's No Opposition Summary Judgment Motion and Order
    </NatureOfAction>
  </LawsuitInfo>
  <CaseType>Standard</CaseType>
  <Remark />
  <CourtInfo>
    <Name />
    <AddressLine1 />
    <AddressLine2 />
    <AddressLine3 />
    <AddressLine4 />
    <City />
    <County />
    <State />
    <Zip />
    <Country />
    <Phone />
    <Fax />
    <Email />
  </CourtInfo>
  <AttorneyInfo>
    <Name />
    <AddressLine1 />
    <AddressLine2 />
    <AddressLine3 />
    <AddressLine4 />
    <City />
    <County />
    <State />
    <Zip />
    <Country />
    <Phone />
    <Fax />
    <Email />
    <LawFirmName />
  </AttorneyInfo>
  <AgencyInfo>
    <Name />
    <AddressLine1 />
    <AddressLine2 />
    <AddressLine3 />
    <AddressLine4 />
    <City />
    <County />
    <State />
    <Zip />
    <Country />
    <Phone />
    <Fax />
    <Email />
  </AgencyInfo>
  <CaseInfo>
    <CaseNumber />
    <Plaintiff />
  </CaseInfo>
  <DocId>123asdf234</DocId>
</SOPDetail>

2 个答案:

答案 0 :(得分:2)

不要忘记使用Xml名称空间

XmlNamespaceManager nsMgr = new XmlNamespaceManager(xDoc.NameTable);
nsMgr.AddNamespace("x", "http://usop.ctadvantage.com/");
XmlNode targetNode = xDoc.SelectSingleNode("/x:SOPDetail/x:TargetInfo/x:DomesticJurisdiction", nsMgr);

答案 1 :(得分:1)

你拼错了:

   /*XmlNode targetNode = xDoc.SelectSingleNode("/SOPDetail/TargetInfo/DomesticJurisidiction");

<强> DomesticJurisidiction

VS

<强> DomesticJurisdiction