C# Linq 父元素和子元素

huangapple go评论67阅读模式
英文:

C# Linq parent and child element

问题

I understand your request. Here is the translated code portion for building a string with seat, game_id, block>id, and customer>id:

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

This code retrieves the necessary elements from the XML and constructs a string with seat, game_id, block>id, and customer>id as you requested.

英文:

Im trying to build a string for mysql insert from XML data, in one case I need to build string with coresponding child and parent elements.

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>

My code:

  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);
                
            }

As game_id is constant its simple to get it but for seat I need also parent block>id and child customer>customerid.

How to build string with seat,game_id,block>id,customer>id?

答案1

得分: -1

Xml 是一项相当古老的技术。我建议使用 Newtonsoft.Json 和 LINQ
使用 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
英文:

Xml is a quite ancient technology. I recommend to use Newtonsoft.Json and 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())));
		}
	}

test

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

答案2

得分: -1

这种方法也可以。

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);
英文:

This approach will work also.

var data = XElement.Load(&quot;file.xml&quot;);

var output = new List&lt;string&gt;();

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

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

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

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

huangapple
  • 本文由 发表于 2023年4月17日 00:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定