C# Linq 父元素和子元素

C# Linq parent and child element

提问人:Olaman 提问时间:4/17/2023 最后编辑:Flydog57Olaman 更新时间:4/17/2023 访问量:94

问:

我试图从XML数据为mysql插入构建一个字符串,在一种情况下,我需要使用对应的子元素和父元素构建字符串。

XML 格式

<data>
  <game>
    <id>173</id>
    <stadium>173</stadium>
    <date>20230408181th</date>
    <title>123</title>
  </game>
  <block>
    <id>1</id>
    <entry>4</entry>
    <title>0</title>
    <visible>1</visible>
    <numbered>1</numbered>
    <seat>
      <id>1</id>
      <intid>123456</intid>
      <abo>N</abo>
      <customer>
 
       <customerid>10295</customerid>
      </customer>
    </seat>
    <seat>
      <id>1</id>
      <intid>1234567</intid>
      <abo>N</abo>
      <customer>
 
       <customerid>10295</customerid>
      </customer>
    </seat>
  </block>
  <block>
    <id>2</id>
    <entry>3</entry>
    <title>0</title>
    <visible>1</visible>
    <numbered>1</numbered>
    <seat>
      <id>1</id>
      <intid>12345678</intid>
      <abo>N</abo>
      <customer>
 
       <customerid>102951</customerid>
      </customer>
    </seat>
    <seat>
      <id>1</id>
      <intid>123456790</intid>
      <abo>N</abo>
      <customer>
 
       <customerid>102951</customerid>
      </customer>
    </seat>
  </block>

我的代码:

  AllocConsole();
            var daneXML = XElement.Load("file.xml");
            string game_id = daneXML.Elements("game").Elements("id").First().Value;
            textbox.Text = game_id;
            var games = daneXML.Elements("game").Select(game => game.Element("id").Value + "," + game.Element("stadium").Value +","+ game.Element("date").Value + ","+game.Element("title").Value);
            foreach (var game in games)
            {
              
                Console.WriteLine(game);

            }

            var blocks = daneXML.Elements("block").Select(block => block.Element("id").Value + "," + block.Element("entry").Value + "," + block.Element("visible").Value + "," + block.Element("numbered").Value);
            foreach (var block in blocks)
            {
                Console.WriteLine(block + "," + game_id);
            }

            var customers = daneXML.Elements("block").Elements("seat").Elements("customer").Select(customer => customer.Element("customerid").Value + "," + customer.Element("name").Value + "," + customer.Element("vorname").Value);
            foreach (var customer in customers)
            {
                Console.WriteLine(customer);
                //insert ignore
            }


            var seats = daneXML.Elements("block").Elements("seat").Select(seat => seat.Element("id").Value + "," + seat.Element("intid").Value + "," + seat.Element("abo").Value + "," + seat.Element("customerid").Value);
            foreach (var seat in seats)
            {
                Console.WriteLine(seat+","+game_id);
                
            }

由于game_id是恒定的,因此很容易获得它,但对于座位,我还需要父块>id 和子 customer>customerid。

如何使用 seat,game_id,block>id,customer>id 构建字符串?

C# XML 链接

评论

0赞 M1sterPl0w 4/17/2023
我会将其序列化为 c# 代码 learn.microsoft.com/en-us/dotnet/api/...,并使用 Linq 搜索 trough 对象,或者其他什么。

答:

-1赞 Serge 4/17/2023 #1

Xml 是一项非常古老的技术。我建议使用 Newtonsoft.Json 和 LINQ

    using Newtonsoft.Json;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);

    var data = JObject.Parse(JsonConvert.SerializeXmlNode(xmlDoc,
                        Newtonsoft.Json.Formatting.None, true));

    List<string> seats = new();
    var gameId = data["game"]["id"].ToString();
    foreach (var item in data["block"].ToArray())
    {
        foreach (var seat in item["seat"])
        {
            seat["customer"] = seat["customer"]["customerid"];
            seats.Add(gameId + "," + item["id"].ToString() + ","
                                + string.Join(",", ((JObject)seat).Properties()
                                     .Select(y => y.Value.ToString())));
        }
    }

测试

foreach (var s in seats) Console.WriteLine(s);

173,1,1,123456,N,10295
173,1,1,1234567,N,10295
173,2,1,12345678,N,102951
173,2,1,123456790,N,102951

评论

0赞 Olaman 4/17/2023
我还必须使用 Newtonsoft.Json.Linq; 添加。它有效,但是如果我在XML树中有更多内容,它会将其放在字符串中,其他解决方案总是给我相同的输出。
-1赞 AVTUNEY 4/17/2023 #2

这种方法也行得通。

var data = XElement.Load("file.xml");

var output = new List<string>();

foreach (var block in data.Elements("block"))
{
    var blockId = block.Element("id").Value;

    foreach (var seat in block.Elements("seat"))
    {
        var seatId = seat.Element("id").Value;
        var intId = seat.Element("intid").Value;
        var abo = seat.Element("abo").Value;
        var customerId = seat.Element("customer").Element("customerid").Value;

        output.Add($"{seatId},{game_id},{blockId},{customerId}");
    }
}

var result = string.Join(Environment.NewLine, output);

评论

0赞 Olaman 4/17/2023
这就是我要找的。在我的版本中,我错过了元素和元素的区别。谢谢!