相关疑难解决方法(0)

从XML文档获取指定的节点值

我在浏览XML文档(使用C#)时遇到问题,并获得所有必要的值.我成功浏览了XML文档中所有指定的XmlNodeLists,成功获取了所有XmlNode值,但我必须在此XmlNodeList之外获取一些值.

例如:

<?xml version="1.0" encoding="UTF-8" ?>
<Element xsi:schemaLocation="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd xsd2009027_kor21.xsd" Kod="370" xmlns="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
/2001/XMLSchema-instance">
    <ANode>
        <BNode>
            <CNode>
                <Example>
                    <Name>John</Name>
                    <NO>001</NO>
                </Example>
            </CNode>
        </BNode>
        <ID>1234</ID>
        <Date>2011-10-01</Date>
    </ANode>
    <ANode>
        <BNode>
            <CNode>
                <Example>
                    <Name>Mike</Name>
                    <NO>002</NO>
                </Example>
            </CNode>
        </BNode>
        <ID>5678</ID>
        <Date>2011-03-31</Date>
    </ANode>
</Element>
Run Code Online (Sandbox Code Playgroud)

这是获取XML文档中每个找到的ANode中节点Name和NO的值的代码:

XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
XmlNodeList xnList = xml.SelectNodes("/Element[@*]/ANode/BNode/CNode");
foreach (XmlNode xn in xnList)
{
  XmlNode example = xn.SelectSingleNode("Example");
    if (example != null)
    {
        string …
Run Code Online (Sandbox Code Playgroud)

c# xml xmldocument

25
推荐指数
1
解决办法
19万
查看次数

使用linq vs xmlDocument从XML字符串中提取数据

我使用xmlDocument方法完成了以下多次,但我想使用更强大的linq到xml方法.但是,我似乎碰壁了.我从twillio/crmText的restful API中获取数据.以下是他们的网站所在网站的链接:http: //crmtext.com/api/docs

这是我的XML字符串:

<response op="getcustomerinfo" status="200" message="ok" version="1.0">
  <customer>
    <totalMsg>3</totalMsg>
    <custId>9008281</custId>
    <custName></custName>
    <timestamp>2015-04-30 16:17:19</timestamp>
    <optinStatus>3</optinStatus>
    <custMobile>6185551212</custMobile>
    <subacct>1st Choice Courier</subacct>
  </customer>
</response>
Run Code Online (Sandbox Code Playgroud)

我需要找出optinStatus.它应该返回3.我使用下面的,返回上面的xml

XDocument xdoc = XDocument.Parse(result1);
Run Code Online (Sandbox Code Playgroud)

我尝试了大约4000种不同的东西,包括:

 IEnumerable<XElement> otinStatus = from el in xdoc.Elements("customer") select el;
          IEnumerable<XElement> otinStatus2 = from el in xdoc.Elements("cusotmer.optinStatus") select el;
           IEnumerable<XElement> otinStatus3 = from el in xdoc.Elements("optinStatus") select el;
Run Code Online (Sandbox Code Playgroud)

所有这些都不会返回任何结果.

请帮助,我知道这是我想念的简单事.提前谢谢你 - 乔

c# xml linq

6
推荐指数
1
解决办法
5083
查看次数

标签 统计

c# ×2

xml ×2

linq ×1

xmldocument ×1