XML:搜索,如果找到节点值,则返回不同的节点

时间:2016-02-05 10:02:57

标签: c# xml xpath

不确定标题是否正确但是...我试图在XML中搜索节点,如果它在那里返回一个不同的节点。

这是我正在使用c#代码;

allianceN.Text = new XElement("eveapi", (from c in dataElements.Elements("result") select new XElement("result", c.Element("factionID"))).Take(1)).Value;

这将返回值:61000575我想使用它来搜索此XML:https://api.eveonline.com/eve/ConquerableStationList.xml.aspx

并返回节点(stationName)值:

  

6-8QLA V - 学习不自由

忘了添加尝试使用的代码:

XmlDocument xxdocoA = new XmlDocument();
xxdocoA.Load(StationListAA);
 XmlNodeList xnodeA = xxdocoA.SelectNodes("/eveapi/result/row[@allianceN]");
                    foreach(XmlNode xnodA in xnodeA)
                    {
                        string NameN = xnodA.Attributes["stationName"].InnerText;
                    }

示例XML,我得到我的StationID值:

    <eveapi version="2"><currentTime>2016-02-05 09:35:05</currentTime><result><characterID></characterID>
<name></name>
<homeStationID>61000575</homeStationID>
<DoB></DoB>
<race></race>
<bloodLineID></bloodLineID>
<bloodLine></bloodLine>
<ancestryID></ancestryID>
<ancestry></ancestry>
<gender></gender>
<corporationName></corporationName>
<corporationID></corporationID>
<allianceName></allianceName>
<allianceID></allianceID>
<factionName/></result></eveapi>

电台名称的例子:

    <eveapi version="2"><currentTime>2016-02-05 08:24:56</currentTime><result>
<rowset name="outposts" key="stationID" columns="stationID,stationName,stationTypeID,solarSystemID,corporationID,corporationName,x,y,z">
<row stationID="61001046" stationName="W-XY4J X - HAKOHELITO" stationTypeID="21646" solarSystemID="30001105" corporationID="98021158" corporationName="corporation federal agents space" x="394820444160" y="-18220769280" z="-6131542302720"/>
<row stationID="61001047" stationName="7MD-S1 XI - Roland's Place" stationTypeID="21645" solarSystemID="30001232" corporationID="98132485" corporationName="Bailiffs" x="-2950319185920" y="-500139909120" z="2101918064640"/>
<row stationID="61001048" stationName="E9KD-N IX - RIP Vile Rat" stationTypeID="21645" solarSystemID="30003694" corporationID="418183520" corporationName="EXPCS Corp" x="1474703155200" y="-198735421440" z="450142740480"/>
<row stationID="61001049" stationName="27-HP0 VI - Brasil TEC Aerospace" stationTypeID="21644" solarSystemID="30000832" corporationID="478292232" corporationName="Brasil TEC" x="785816985600" y="4615004160" z="-576230154240"/>
<row stationID="61001050" stationName="L-5JCJ VIII - RUSTARPORT" stationTypeID="21645" solarSystemID="30002142" corporationID="98139285" corporationName="Love All Woodland Nymphs" x="-2645266391040" y="533918883840" z="-6264785510400"/>
<row stationID="61001051" stationName="Y5-E1U VII - The Dibby Parlour" stationTypeID="21646" solarSystemID="30000729" corporationID="98415166" corporationName="Zero.Four Holdings" x="-1276127109120" y="32335749120" z="4544564551680"/>
<row stationID="61001052" stationName="F2-NXA II - The Valley of Eternal Night" stationTypeID="21645" solarSystemID="30000695" corporationID="98025393" corporationName="Fleet of the Eternal Night" x="-47411404800" y="7704944640" z="139655454720"/>
<row stationID="61001053" stationName="3-SFWG V - DRASY Jobcenter" stationTypeID="21644" solarSystemID="30001212" corporationID="98278570" corporationName="Das Raumfahrer Syndikat" x="922842685440" y="85610864640" z="-2470096035840"/>
<row stationID="61001054" stationName="UQ-PWD III - Home of the Demon Clown Legion" stationTypeID="21646" solarSystemID="30001208" corporationID="261632485" corporationName="Fink Operations" x="-49921351680" y="4879687680" z="-123196661760"/>
<row stationID="61001055" stationName="1EO-OE V - Traffic island" stationTypeID="21646" solarSystemID="30003211" corporationID="98064360" corporationName="Despair fellow pilots" x="-1619052011520" y="28839813120" z="84353310720"/>
<row stationID="61001056" stationName="G-4H4C III - Kazeboshka" stationTypeID="21646" solarSystemID="30003161" corporationID="98396853" corporationName="Lavina Holding" x="-54066094080" y="3877724160" z="146350448640"/>
<row stationID="61001057" stationName="G7AQ-7 VIII - Providence STD Clinic" stationTypeID="21644" solarSystemID="30003713" corporationID="101116365" corporationName="W.A.S.P" x="1285768519680" y="-36475576320" z="-3470034739200"/>
<row stationID="61001058" stationName="FA-DMO X - VOTE THE JUDGE FOR C S M XI" stationTypeID="21646" solarSystemID="30000233" corporationID="1667598016" corporationName="SRBI Holding" x="-1172241408000" y="235930214400" z="326454435840"/></result></eveapi>

这些文件很大,所以这只是它看起来的片段,如果关闭标签错误,我很抱歉。

3 个答案:

答案 0 :(得分:0)

所以你有&#34; 61000575&#34;并希望具有stationID =&#34; 61000575&#34;的节点的stationName。

这可能是//row[@stationID='61000575']/@stationName或:

(new XDocument(...)).Root.Descendants("row").Where(row=>row.Attribute("stationID").Value == "61000575").Single().Attribute("stationName")

答案 1 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication75
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml1 = 
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                    "<Root>" +
                      "<eveapi version=\"2\">" +
                        "<currentTime>2016-02-05 09:35:05</currentTime>" +
                        "<result>" +
                          "<characterID></characterID>" +
                          "<name></name>" +
                          "<homeStationID>61001046</homeStationID>" +
                          "<homeStationID>61001047</homeStationID>" +
                          "<DoB></DoB>" +
                        "</result>" +
                      "</eveapi>" +
                    "</Root>";

            XDocument doc1 = XDocument.Parse(xml1);
            string[] stationIDs = doc1.Descendants("homeStationID").Select(x => x.Value).ToArray();

            string xml2 =
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                  "<eveapi version=\"2\">" +
                    "<currentTime>2016-02-05 08:24:56</currentTime>" +
                    "<result>" +
                      "<rowset name=\"outposts\" key=\"stationID\" columns=\"stationID,stationName,stationTypeID,solarSystemID,corporationID,corporationName,x,y,z\">" +
                        "<row stationID=\"61001046\" stationName=\"W-XY4J X - HAKOHELITO\" stationTypeID=\"21646\" solarSystemID=\"30001105\" corporationID=\"98021158\" corporationName=\"corporation federal agents space\" x=\"394820444160\" y=\"-18220769280\" z=\"-6131542302720\"/>" +
                        "<row stationID=\"61001047\" stationName=\"7MD-S1 XI - Roland's Place\" stationTypeID=\"21645\" solarSystemID=\"30001232\" corporationID=\"98132485\" corporationName=\"Bailiffs\" x=\"-2950319185920\" y=\"-500139909120\" z=\"2101918064640\"/>" +
                        "<row stationID=\"61001048\" stationName=\"E9KD-N IX - RIP Vile Rat\" stationTypeID=\"21645\" solarSystemID=\"30003694\" corporationID=\"418183520\" corporationName=\"EXPCS Corp\" x=\"1474703155200\" y=\"-198735421440\" z=\"450142740480\"/>" +
                       "</rowset>" +
                    "</result>" +
                  "</eveapi>";

            XDocument doc2 = XDocument.Parse(xml2);
            XElement[] stationIDsNodes = stationIDs.Select(x => doc2.Descendants("row").Where(y => y.Attribute("stationID").Value == x)).SelectMany(z => z).ToArray();

        }
    }
}

答案 2 :(得分:0)

我知道回答我自己的问题是不好的,但是只是因为其他人需要这种信息,我就是这样做的:

string string1 = new XElement("eveapi", (from c in dataElements.Elements("result") select new XElement("result", c.Element("homeStationID"))).Take(1)).Value;

 XmlDocument STIDD = new XmlDocument();
                        STIDD.Load(StationListAA);
                        XmlNodeList xnodeAA = STIDD.SelectNodes("eveapi/result/rowset/row[@stationID =" + string1 + "]");
                        foreach(XmlNode xxnodAA in xnodeAA)
                        {
                            allianceN.Text = xxnodAA.Attributes["stationName"].InnerText;
                        }     

感谢大家的帮助!