Comment utiliser le même XMLAttribute pour plusieurs propriétés?
J'ai la classe wrapper ci-dessous.
[Serializable]
public class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public string Year { get; set; }
public static void Serialize()
{
string fileName = "data.xml";
Console.WriteLine("Enter Make, Model & Year");
var vehicle = new Vehicle
{
Make = "Honda",
Model = "H1245",
Year = "1975"
};
//Serialization
var document = new XmlDocument();
var xml = new XmlSerializer(typeof(Vehicle));
TextWriter writer = new StreamWriter(fileName);
xml.Serialize(writer, vehicle);
writer.Close();
//Attribution
if (File.Exists(fileName))
{
document.Load(fileName);
var nodeList = document.SelectNodes("//Vehicle//*");
var attribute1 = document.CreateAttribute("applicableTo");
attribute1.Value = "Common";
var attribute2 = document.CreateAttribute("applicableTo");
attribute2.Value = "Cfpd";
var attribute3 = document.CreateAttribute("applicableTo");
attribute3.Value = "Markel";
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeList[i].InnerText.Equals("Honda"))
((XmlElement)(nodeList[i])).SetAttributeNode(attribute1);
if (nodeList[i].InnerText.Equals("H1245"))
((XmlElement)(nodeList[i])).SetAttributeNode(attribute2);
if (nodeList[i].InnerText.Equals("1975"))
((XmlElement)(nodeList[i])).SetAttributeNode(attribute3);
}
document.Save(Console.Out);
}
}
Ma sortie souhaitée est comme ci-dessous. Cependant, j'obtiens le même résultat que XML que ci-dessous.
<?xml version="1.0" encoding="utf-8"?>
<Vehicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Make applicableTo="Common">Honda</Make>
<Model applicableTo="Cfpd">H1245</Model>
<Year applicableTo="Markel">1975</Year>
</Vehicle>
Voici mon problème,
Comment dois - je l'attribut applicableToensemble à faire, propriétés modèle et l' année sans avoir à les créer comme attribute1, attribute2, attribute3encore des valeurs à affecter en fonction de l'entrée affectée aux propriétés? Ce qui signifie que , si Makeest Hondala sortie XML doit être <Make applicableTo="Common">Honda</Make>de même.
XMLAttribute ne peut être défini que sur une propriété dans une classe, ce qui n'est pas idéal lorsqu'il s'agit d'un grand nombre de classes. Et ainsi SetAttributeNodejette une erreur -
Le nœud 'Attribut' ne peut pas être inséré car il s'agit déjà d'un attribut d'un autre élément.
Veuillez aider.
Veuillez noter que le format de ma sortie ne devrait pas changer.
Réponses
Utilisation de Xml Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Make", typeof(string));
dt.Columns.Add("Make Attribute", typeof(string));
dt.Columns.Add("Model", typeof(string));
dt.Columns.Add("Model Attribute", typeof(string));
dt.Columns.Add("Year", typeof(string));
dt.Columns.Add("Year Attribute", typeof(string));
dt.Rows.Add(new object[] { "Honda", "Common", "H1245", "Cfpd", "1975", "Markel" });
dt.Rows.Add(new object[] { "Honda", "Common", "H1245", "Cfpd", "1975", "Markel" });
dt.Rows.Add(new object[] { "Honda", "Common", "H1245", "Cfpd", "1975", "Markel" });
Vehicle.Serialize(dt, FILENAME);
}
}
public class Vehicle
{
public static void Serialize(DataTable dt, string fileName)
{
string ident =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Vehicles xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"</Vehicles>";
XDocument doc = XDocument.Parse(ident);
XElement xVehicles = doc.Root;
foreach (DataRow row in dt.AsEnumerable())
{
XElement xVehicle = new XElement("Vehicle", new object[] {
new XElement("Make", new object[] {new XAttribute("applicableTo", row.Field<string>("Make Attribute")), row.Field<string>("Make")}),
new XElement("Make", new object[] {new XAttribute("applicableTo", row.Field<string>("Model Attribute")), row.Field<string>("Model")}),
new XElement("Make", new object[] {new XAttribute("applicableTo", row.Field<string>("Year Attribute")), row.Field<string>("Year")})
});
xVehicles.Add(xVehicle);
}
doc.Save(fileName);
}
}
}