1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// Gestion des Namespaces
if (xdoc.Root.DescendantsAndSelf().Where(m => m.Attribute("xmlns") != null).Count() > 0)
{
Dictionary<string, string> lstPrefix = new Dictionary<string, string>();
int cpt = 0;
xdoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));
foreach (XElement xel in xdoc.Root.DescendantsAndSelf().Where(n => n.Attribute("xmlns") != null))
{
string ns = xel.Attribute("xmlns").Value;
if (ns != "")
{
if (!lstPrefix.Keys.Contains(ns))
{
if (xel == xdoc.Root)
xel.Attribute("xmlns").Remove();
lstPrefix.Add(ns, "n" + (++cpt).ToString());
xdoc.Root.Add(new XAttribute(XNamespace.Xmlns + lstPrefix[ns], ns));
}
}
}
} |
Partager