如何从 xml 文件中获取嵌套元素

How to get nested element from xml file

我尝试return XML中的一些嵌套值file.And我想return每张桌子的价值,邮政编码,重量和价值。

所以我有这个 xml 文件:

<?xml version="1.0"?>
<Container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>684</Id>
  <dateCool>2011-08-22T00:00:00+05:00</dateCool>
  <desks>
    <desk>
      <Sender>
        <Name>Jan</Name>
        <Address>
          <Street>bood</Street>
          <HouseNumber>22</HouseNumber>
          <PostalCode>2402AE</PostalCode>
          <City>Al Rijn</City>
        </Address>
      </Sender>
      <participant>
        <Name>Piet</Name>
        <Address>
          <Street>Schan</Street>
          <HouseNumber>22</HouseNumber>
          <PostalCode>2497AV</PostalCode>
          <City>Den Haag</City>
        </Address>
      </participant>
      <Weight>0.02</Weight>
      <Value>0.0</Value>
    </desk>
    <desk>
      <Sender xsi:type="Company">
        <Name>ykken groot B.V.</Name>
        <Address>
          <Street>ILSY-PLantsoen</Street>
          <HouseNumber>1</HouseNumber>
          <PostalCode>2497GA</PostalCode>
          <City>Den Haag</City>
        </Address>
        <CcNumber>65465424</CcNumber>
      </Sender>
      <participant>
        <Name>Aad</Name>
        <Address>
          <Street>Korenbloemkamp</Street>
          <HouseNumber>22</HouseNumber>
          <PostalCode>2353HS</PostalCode>
          <City>Leiderdorp</City>
        </Address>
      </participant>
      <Weight>2.0</Weight>
      <Value>0.0</Value>
    </desk>
    <desk>
      <Sender xsi:type="Company">
        <Name>seti</Name>
        <Address>
          <Street>ILSY-PLantsoen</Street>
          <HouseNumber>1</HouseNumber>
          <PostalCode>2497GA</PostalCode>
          <City>Den Haag</City>
        </Address>
        <CcNumber>65465424</CcNumber>
      </Sender>
      <participant>
        <Name>Martijn</Name>
        <Address>
          <Street>Burgemeester Roosstraat</Street>
          <HouseNumber>33</HouseNumber>
          <PostalCode>3035 AC</PostalCode>
          <City>Rotterdam</City>
        </Address>
      </participant>
      <Weight>100.0</Weight>
      <Value>2000.0</Value>
    </desk>
    <desk>
      <Sender xsi:type="Company">
        <Name>Aad</Name>
        <Address>
          <Street>Korenbloemkamp</Street>
          <HouseNumber>22</HouseNumber>
          <PostalCode>2353HS</PostalCode>
          <City>Leiderdorp</City>
        </Address>
      </Sender>
      <participant>
        <Name>Martijn</Name>
        <Address>
          <Street>Burge Roosaat</Street>
          <HouseNumber>33</HouseNumber>
          <PostalCode>3035 AC</PostalCode>
          <City>Rotterdam</City>
        </Address>
      </participant>
      <Weight>11</Weight>
      <Value>500</Value>
    </desk>
  </desks>
</Container>

我的代码如下所示:

static void Main(string[] args)
        {
            FileStream fs = new FileStream("Container.xml", FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            string s = sr.ReadToEnd();

            sr.Close();
            fs.Close();

            List<Dictionary<string, string>> orderList = new List<Dictionary<string, string>>();
            XDocument xDocument = XDocument.Parse(s);

            //Get all nodes
            List<XNode> xNodes = xDocument.DescendantNodes().ToList();
            foreach (XNode node in xNodes)
            {
                XElement element = node as XElement;
                if (element.Name != "desk") continue;
                Dictionary<string, string> dict = new Dictionary<string, string>();

                //For each orderProperty, get all attributes
                foreach (XAttribute attribute in element.Attributes())
                {
                    dict.Add(attribute.Name.ToString(), attribute.Value);
                }
                orderList.Add(dict);
            }


            foreach (Dictionary<string, string> dict in orderList)
            {
                foreach (string key in dict.Keys)
                {
                    Console.Write(key + ": " + dict[key] + ", ");
                }
                Console.Write("\n");
            }

        }

但我每次都会收到这个错误:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

这是因为一些xnodes是空的

使用此代码

有效:)

static void Main(string[] args)
{
        var s = string.Empty;

        using (var fs = new FileStream("Container.xml", FileMode.Open))
        {
            using (var sr = new StreamReader(fs))
            {
                s = sr.ReadToEnd();
            }
        }

        var orderList = new List<Dictionary<string, string>>();
        var xDocument = XDocument.Parse(s);


        var xNodes = xDocument.DescendantNodes().ToList();
        foreach (var node in xNodes)
        {
            var element = node as XElement;
            if (element == null) continue;
            if (element.Name != "desk") continue;
            var dict = new Dictionary<string, string>();


            foreach (var deskNode in element.Nodes())
            {
                var deskElement = deskNode as XElement;
                if (deskElement == null) continue;

                dict.Add(deskElement.Name.ToString(), deskElement.Value);
            }
            orderList.Add(dict);
        }


        foreach (var dict in orderList)
        {
            foreach (string key in dict.Keys)
            {
                Console.Write(key + ": " + dict[key] + ", ");

            }
            Console.Write("\n");
        }

        Console.ReadKey();
 }

我更改了你的第二个 foreach 循环 element.Attributes() return null