如何重新编码xml

本文关键字:xml 编码 何重新 | 更新日期: 2023-09-27 17:51:06

正在处理xml重新排序我的XML文件如下:

 <Subjects>
   <Subject>
     <Name></Name>
     <Height></Height>
     <Addresss>
        <Address>
           <City>AB</City>
        </Address>
     </Addresses>
   </Subject>
   <Subject>
     <Name></Name>
     <Height></Height>
     <Addresss>
        <Address>
           <City>CD</City>
        </Address>
     </Addresses>
   </Subject>
 </Subjects>

现在我想通过向主题

添加地址引用来创建如下XML
<Order>
 <Subjects>
   <Subject>
     <Name></Name>
     <Height></Height>
     <Address ref="A1"/>
   </Subject>
   <Subject>
     <Name></Name>
     <Height></Height> 
     <Address ref="A2"/>      
   </Subject>
 </Subjects>
 <Addresss>
     <Address id="A1">
        <City>AB</City>
      </Address>      
      <Address id="A2">
        <City>AB</City>
      </Address>
  </Addresses>
</Order>

我如何用SQL或LINQ完成这个?

如何重新编码xml

使用XmlDocument类,您可以使用ImportNode方法将元素从一个文档转移到另一个文档,如下所示:

var index     = 0;
var output    = new XmlDocument();
var order     = output.AppendChild(output.CreateElement("Order"    ));
var subjects  = order .AppendChild(output.CreateElement("Subjects" ));
var addresses = order .AppendChild(output.CreateElement("Addresses"));
foreach (XmlElement subject in input.SelectNodes("/Subjects/Subject"))
{
    index++;
    var address = (XmlElement)subject.RemoveChild(subject.SelectSingleNode("Addresses"));
        address = (XmlElement)address.FirstChild;   // Addresses to Address
        address.SetAttribute("id", index.ToString());
    var newAddress = (XmlElement)subject.AppendChild(input.CreateElement("Address"));
        newAddress.SetAttribute("ref", index.ToString());
    subjects .AppendChild(output.ImportNode(subject, true));
    addresses.AppendChild(output.ImportNode(address, true));
}
output.Save(Console.Out);
Console.ReadLine();

使用XML linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
            "<Subjects>" +
               "<Subject>" +
                 "<Name></Name>" +
                 "<Height></Height>" +
                 "<Addresses>" +
                    "<Address>" +
                       "<City>AB</City>" +
                    "</Address>" +
                 "</Addresses>" +
               "</Subject>" +
               "<Subject>" +
                 "<Name></Name>" +
                 "<Height></Height>" +
                 "<Addresses>" +
                    "<Address>" +
                       "<City>CD</City>" +
                    "</Address>" +
                 "</Addresses>" +
               "</Subject>" +
             "</Subjects>";
            XDocument doc = XDocument.Parse(input);
            foreach (XElement subject in doc.Descendants("Subject"))
            {
                string address = subject.Element("Addresses").Element("Address").Element("City").Value;
                XElement newAddress = new XElement("Address");
                XAttribute newAttribute = new XAttribute("ref", address);
                newAddress.Add(newAttribute);
                subject.Add(newAddress);
                subject.Element("Addresses").Remove();
            }
        }
    }
}
​