Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中解析XML文档的问题_C#_.net_Xml - Fatal编程技术网

C# 在c中解析XML文档的问题

C# 在c中解析XML文档的问题,c#,.net,xml,C#,.net,Xml,我试图从XML文档中的特定元素获取innertext,并通过字符串传递到中,但我不明白为什么它找不到任何节点 这段代码运行良好,但不会进入任何一个FOREACH循环,因为ocNodesCompany和ocnodesorg都有xero元素。为什么GetElementsByTagName找不到节点 顺便说一句,我也试过: XmlNodeList ocNodesOrgs = thisXmlDoc.SelectNodes("//OpenCalaisSimple/CalaisSimpleOutputFor

我试图从XML文档中的特定元素获取innertext,并通过字符串传递到中,但我不明白为什么它找不到任何节点

这段代码运行良好,但不会进入任何一个FOREACH循环,因为ocNodesCompany和ocnodesorg都有xero元素。为什么GetElementsByTagName找不到节点

顺便说一句,我也试过:

XmlNodeList ocNodesOrgs = thisXmlDoc.SelectNodes("//OpenCalaisSimple/CalaisSimpleOutputFormat/Company")
代码:

我的XML字符串是:

<!--Use of the Calais Web Service is governed by the Terms of Service located at http://www.opencalais.com. By using this service or the results of the service you agree to these terms of service.--><!-- Company: BBC,T-mobile,Vodafone,GE, IndustryTerm: open calais services, Organization: Federal Bureau of Investigation,Red Cross,Greenpeace,Royal Navy,-->

<OpenCalaisSimple>
    <Description>
        <calaisRequestID>38cb8898-48ba-85ff-12e9-f8d629568428</calaisRequestID>
        <id>http://id.opencalais.com/lt0Hf8XWIr2DNIJzNlaXlA</id>
        <about>http://d.opencalais.com/dochash-1/ff929eb2-de43-3ed1-8ee4-6109abf6bf77</about>
        <docTitle/>
        <docDate>2011-03-10 06:36:08.646</docDate>
        <externalMetadata/>
    </Description>
    <CalaisSimpleOutputFormat>
        <Company count="1" relevance="0.603" normalized="British Broadcasting Corporation">BBC</Company>
        <Company count="1" relevance="0.603" normalized="T-MOBILE NETHERLANDS HOLDING B.V.">T-mobile</Company>
        <Company count="1" relevance="0.603" normalized="Vodafone Group Plc">Vodafone</Company>
        <Company count="1" relevance="0.603" normalized="General Electric Company">GE</Company>
        <IndustryTerm count="1" relevance="0.603">open calais services</IndustryTerm>
        <Organization count="1" relevance="0.603">Red Cross</Organization>
        <Organization count="1" relevance="0.603">Greenpeace</Organization>
        <Organization count="1" relevance="0.603">Royal Navy</Organization>
        <Topics>
            <Topic Taxonomy="Calais" Score="0.899">Human Interest</Topic>
            <Topic Taxonomy="Calais" Score="0.694">Technology_Internet</Topic>
        </Topics>
    </CalaisSimpleOutputFormat>
</OpenCalaisSimple>

似乎应该使用XPath查询来获取想要接收的元素。您可以阅读它

注意,Microsoft建议您也使用XPath,这是他们关于GetElementsByTag方法的帮助页面,注意中间的注释,建议使用SelectNodes,即XPath

使用XPath编写的方法的一个变体是:

public static ArrayList getTwitterHandles(String ocXML)
{
    ArrayList thisList = new ArrayList();
    XmlDocument thisXmlDoc = new XmlDocument();
    thisXmlDoc.LoadXml(ocXML);

    //get Companies
    XmlNodeList ocNodesCompany = thisXmlDoc.SelectNodes("//Company");
    foreach (XmlElement element in ocNodesCompany)
    {
        thisList.Add(element.InnerText);
    }

    //Get Organisations
    XmlNodeList ocNodesOrgs = thisXmlDoc.SelectNodes("//Organization");
    foreach (XmlElement element in ocNodesOrgs)
    {
        thisList.Add(element.InnerText);
    }

    //Get Organisations

    return thisList;
}
请注意,上面实现了我认为您在示例中拥有的功能,这与您尝试的xpath不太一样。在XPath中,基本上//表示任何父节点,因此//Company将拾取您传入的根中名为Company的任何子节点

如果只需要特定的公司节点,则可以更具体:

    XmlNodeList ocNodesCompany = thisXmlDoc.SelectNodes("//Company");
变成

    XmlNodeList ocNodesCompany = thisXmlDoc.SelectNodes("/OpenCalaisSimple/CalaisSimpleOutputFormat/Company");
请注意,关键区别在于开头只有一个正斜杠

我刚刚测试了这两种变体,它们工作得很好

如果您正在处理XML文件,那么我强烈建议您阅读XPath,并成为其大师,它非常方便,可以让您快速编写代码,通过XML文件进行解析,并准确地选择您需要的内容,尽管我应该补充一点,它不是唯一的方法,当然也不适合所有情况:

希望这有帮助。

您也可以使用System.Xml.Linq命名空间中的XDocument。下面的代码片段几乎与您的代码相同。返回类型是List而不是ArrayList


这应该是一个评论,而不是一个答案。当我开始回答这个问题时,这个问题并没有被完全描述。我没有足够的业力来添加评论,所以我请造物主纠正他的问题。在问题被纠正后,我对我的答案也做了同样的修改。我倒转了我的反对票。编辑您的评论,使其生效。似乎您应该使用XPath查询来获取您想要接收的元素。您可以阅读[here][1][1]:这可能是一个愚蠢的问题,但是您是否调查了XML文档对象的InnerXml,以验证它作为参数传递时是否正确加载?我测试了这一点,代码似乎工作正常。检查ocXML参数中的数据是否正确,并验证它是否正确加载。我也对它进行了测试。我打电话给getTwitterHandles-BBC T-mobile沃达丰GE红十字会绿色和平皇家NavyHmm后收到了以下物品。。。认为它加载正确…使用VS2010进行调试,数据似乎就在那里!显然不是。一定是丢了什么,谢谢。我想你是对的,多读一些选项。我是处理XML的新手。
    XmlNodeList ocNodesCompany = thisXmlDoc.SelectNodes("/OpenCalaisSimple/CalaisSimpleOutputFormat/Company");
public static List<string> getTwitterHandles(String ocXml)
    var xml = XDocument.Parse(ocXml);
    var list = xml.Descendants("Company")
            .Concat(xml.Descendants("Organization"))
            .Select(element => element.Value)
            .ToList();
    return list;
}