0

How do I add the char : to an XML element attribute?

Here is how the output should look like

<alto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.loc.gov/standards/alto/ns-v3#" 
      xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v3# http://www.loc.gov/standards/alto/v3/alto-3-1.xsd" SCHEMAVERSION="3.1" 
      xmlns:xlink="http://www.w3.org/1999/xlink">

and so far this is my code

var z = doc.Descendants("alto").First();
XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
z.Add(new XAttribute(XNamespace.Xmlns + "xsi", ns.NamespaceName));

I tried this code, but it gives me error

new XAttribute("xmlns", "http://www.loc.gov/standards/alto/ns-v3#")

and here is the error message:

The prefix '' cannot be redefined from '' to 'http://www.loc.gov/standards/alto/ns-v3#' within the same start element tag

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pdf to image
  • 369
  • 6
  • 23
  • 1
    How should your output XML look like? And to what attribute should this char be added? – zx485 Jan 18 '17 at 02:56
  • What is the error message? – Ilian Jan 18 '17 at 03:32
  • @zx485 the output xml is on the question sir – pdf to image Jan 18 '17 at 03:41
  • @IlianPinzon i updated the question and add the error message, thank you – pdf to image Jan 18 '17 at 03:43
  • Possible duplicate of [The prefix " cannot be redefined from " to within the same start element tag](http://stackoverflow.com/questions/23698767/the-prefix-cannot-be-redefined-from-to-url-within-the-same-start-element-t) – Ilian Jan 18 '17 at 03:48
  • Those are **XML namespace** definitions - you cannot just *add a `:`* to your attribute name - you need to understand the concepts behind XML namespaces and handle them accordingly – marc_s Jan 18 '17 at 06:05

1 Answers1

0

You should put default namespace last. For complex namespaces like yours I usually just parse the string like code below :

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 xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<alto xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                  " xmlns=\"http://www.loc.gov/standards/alto/ns-v3#\"" +
                  " xsi:schemaLocation=\"http://www.loc.gov/standards/alto/ns-v3# http://www.loc.gov/standards/alto/v3/alto-3-1.xsd\" SCHEMAVERSION=\"3.1\"" +
                  " xmlns:xlink=\"http://www.w3.org/1999/xlink\">" +
                 "</alto>";

            XDocument doc = XDocument.Parse(xml);
            XElement root = doc.Root;

            XNamespace ns = root.GetDefaultNamespace();
            XNamespace nsXsi = root.GetNamespaceOfPrefix("xsi");
            XNamespace nsSchemaLocation = root.GetNamespaceOfPrefix("schemaLocation");
            XNamespace nsXlink = root.GetNamespaceOfPrefix("xlink");

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20