如何使用LINQ-to-XML生成顺序属性值?

时间:2018-04-12 16:11:05

标签: c# linq-to-xml

我有一些xml文件,格式如下所示,或者可能略有不同

<?xml version="1.0"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
    <TITLE>Computer Parts</TITLE>
    <ASST>
        <PART no="xs@@">
            <ITEM>Motherboard</ITEM>
            <MANUFACTURER>ASUS</MANUFACTURER>
            <MODEL ver="v-@@@@">P3B-F</MODEL>
            <COST>123.00</COST>
        </PART>
        <PART no="xs@@">
            <ITEM>Video Card</ITEM>
            <MANUFACTURER>ATI</MANUFACTURER>
            <MODEL ver="v-@@@@">All-in-Wonder Pro</MODEL>
            <COST>160.00</COST>
        </PART>
        <PART no="xs@@">
            <ITEM>Sound Card</ITEM>
            <MANUFACTURER>Creative Labs</MANUFACTURER>
            <MODEL ver="v-@@@@">Sound Blaster Live</MODEL>
            <COST>80.00</COST>
        </PART>
    </ASST>
</PARTS>

我尝试按顺序方式生成节点<PART>的属性值,即<MODEL>,即 ver 从值1开始,然后从下一个<PART>节点增加+1。 所以,基本上所需的输出应该看起来像这样

<?xml version="1.0"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
    <TITLE>Computer Parts</TITLE>
    <ASST>
        <PART no="xs1">
            <ITEM>Motherboard</ITEM>
            <MANUFACTURER>ASUS</MANUFACTURER>
            <MODEL ver="v-1">P3B-F</MODEL>
            <COST>123.00</COST>
        </PART>
        <PART no="xs2">
            <ITEM>Video Card</ITEM>
            <MANUFACTURER>ATI</MANUFACTURER>
            <MODEL ver="v-2">All-in-Wonder Pro</MODEL>
            <COST>160.00</COST>
        </PART>
        <PART no="xs3">
            <ITEM>Sound Card</ITEM>
            <MANUFACTURER>Creative Labs</MANUFACTURER>
            <MODEL ver="v-3">Sound Blaster Live</MODEL>
            <COST>80.00</COST>
        </PART>
    </ASST>
</PARTS>

那么使用LINQ-to-XML执行此操作的最简单方法是什么?

我当时想要

string filecontent=File.ReadAllText(@"C:\Users\Desktop\12-apr.xml");
XDocument xml=XDocument.Parse(filecontent);
var parts=xml.Descendants("PART");
var models=xml.Descendants("PART").Elements("MODEL");
foreach (var part in parts)
{
    for (int i = 0; i < parts.Count(); i++)
    {
        part.Attribute("no").Value="xs"+i.ToString();
    }

}
foreach (var model in models)
{
    for (int i = 0; i < parts.Count(); i++)
    {
        model.Attribute("ver").Value="v-"+i.ToString();
    }

}
xml.Save(@"C:\Users\Desktop\12-apr.xml");
Console.ReadLine();

但它不按我打算的方式工作.. 我的代码生成的输出

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd"[]>
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
  <TITLE>Computer Parts</TITLE>
  <ASST>
    <PART no="xs2">
      <ITEM>Motherboard</ITEM>
      <MANUFACTURER>ASUS</MANUFACTURER>
      <MODEL ver="v-2">P3B-F</MODEL>
      <COST>123.00</COST>
    </PART>
    <PART no="xs2">
      <ITEM>Video Card</ITEM>
      <MANUFACTURER>ATI</MANUFACTURER>
      <MODEL ver="v-2">All-in-Wonder Pro</MODEL>
      <COST>160.00</COST>
    </PART>
    <PART no="xs2">
      <ITEM>Sound Card</ITEM>
      <MANUFACTURER>Creative Labs</MANUFACTURER>
      <MODEL ver="v-2">Sound Blaster Live</MODEL>
      <COST>80.00</COST>
    </PART>
  </ASST>
</PARTS>

帮助

1 个答案:

答案 0 :(得分:1)

你有没有明显原因的嵌套循环......你为每个元素多次设置属性,而不是每个元素设置一次属性。

一旦你摆脱了嵌套循环,它就会很简单。如果您将查询具体化为列表,则会更简单。这是一个产生所需输出的完整示例:

using System;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        var xml = XDocument.Load("input.xml");
        var parts = xml.Descendants("PART").ToList();
        var models = xml.Descendants("PART").Elements("MODEL").ToList();

        for (int i = 0; i < parts.Count; i++)
        {
            parts[i].Attribute("no").Value = $"xs{i+1}";
        }

        for (int i = 0; i < models.Count; i++)
        {
            models[i].Attribute("ver").Value = $"v-{i+1}";
        }

        xml.Save("output.xml");
    }
}