需要从XML文件C#中选择数据

时间:2017-05-02 20:40:51

标签: c# xml linq-to-xml

我尝试做的是从我的XML文件中获取数据,该文件已与其他两个文件合并,并从该文件中选择每个场所,并尝试将值添加到列表中,以便我可以进一步操作它。 / p>

这是我的一个XML文件

library ieee;
use ieee.std_logic_1164.all;

entity slm_tb is
end entity;

architecture foo of slm_tb is
    -- Input ports
    signal clk:         std_logic := '1';
    signal message:     std_logic := '0';
    signal display:     std_logic := '0';
    signal start:       std_logic := '1';
    -- Output ports
    signal seg14:       std_logic_vector (13 downto 0);
    signal lengthLED:   std_logic;
begin

DUT:
    entity work.sauvezlesmorses
        port map (
            clk => clk,
            message => message,
            display => display,
            start   => start,
            seg14   => seg14,
            lengthLED => lengthLED
        );

-- force -freeze sim:/sauvezlesmorses/clk 1 0, 0 {25000000000 ps} -r {50 ms}
-- force -freeze sim:/sauvezlesmorses/display 0 0, 1 {9000000000000 ps} -r {18 sec}
-- force -freeze sim:/sauvezlesmorses/message 0 0, 1 {3200000000000 ps} -r {6.4 sec}
-- force -freeze sim:/sauvezlesmorses/start 1 0 -cancel {0.5 sec}
-- run 40 sec

-- stimulus generators:
CLOCK:
    process
    begin
        wait for 25 ms;
        clk <= not clk;
        if now > 40 sec then
            wait;
        end if;
    end process;
DISP:
    process
    begin
        wait for 9 sec;
        display <= not display;
        if now > 35 sec then  -- stop simulation at 40 sec
            wait;
        end if;
    end process;
MSG:
    process
    begin
        wait for 3.2 sec;
        message <= not message;
        if now > 35 sec then
            wait;
        end if;
    end process;
ST:
    process
    begin
        wait for 0.5 sec;
        start <= 'U';
        wait;
    end process;
end architecture;

我需要能够选择场地名称并将其保存到列表中。这是我到目前为止所得到的:

<?xml version="1.0" encoding="utf-8" ?>
<Funrun>
<Venue name="Roker Park">
<Runner charity="Cancer Research">
  <Firstname>Roger</Firstname>
  <Lastname>Malibu</Lastname>
  <Sponsorship>550</Sponsorship>
</Runner>
<Runner charity="Arthritis UK">
  <Firstname>Adam</Firstname>
  <Lastname>White</Lastname>
  <Sponsorship>340</Sponsorship>
</Runner>
</Venue>
</Funrun >

2 个答案:

答案 0 :(得分:0)

List<string> xmlFilePaths = new List<string>
{
    @"Path\\SomeJson.txt",
    @"Path\\SomeJson1.txt"
};

var venues = xmlFilePaths.Select(fp => XDocument.Load(fp).Descendants("Venue")?.FirstOrDefault()?.Attribute("name")?.Value).Distinct().ToList();

答案 1 :(得分:0)

我最简单的方法是在他们所属的XElements中包含Name和Charity。 我建议你做的是首先重新格式化你的文档,使它看起来像这样:

<Funrun>
    <Venue>
        <Name>Roker Park</Name>
        <Runner1>
            <charity>Cancer Research</charity>
            <Firstname>Roger</Firstname>
            <Lastname>Malibu</Lastname>
            <Sponsorship>550</Sponsorship>
        </Runner1>
        <Runner2>
            <charity>Arthritis UK</charity>
            <Firstname>Adam</Firstname>
            <Lastname>White</Lastname>
            <Sponsorship>340</Sponsorship>
        </Runner2>
    </Venue>
</Funrun >

请注意,通过组合&#34; Funrun&#34;下的所有元素,您可以更简单。 (例如:&#34; Venue&#34;)并且无需切换文档就可以遍历所有这些文件。

接下来转到C#:

List<string> VenueNames = new List<string>();
var doc = XDocument.Load("XMLFile1.xml");
var doc2 = XDocument.Load("XMLFile2.xml");
var doc3 = XDocument.Load("XMLFile3.xml");

foreach (XElement element in doc.Root.Descendants("Venue"))
        {
            VenueNames.Add(element.Element("Name").Value.ToString());
        }
//Copy paste this code for each document you would like to search through, though of course change "doc" to say, "doc2".

所以,真正快速,这段代码将做的是它将首先打开XDocument中的Root元素。它会找到名称为&#34; Name&#34;的该元素的后代,并且对于每个元素,它会将其值作为字符串复制到您的列表中。