4

Question update: im very sorry if my question is not clear

here is the code im using right now

XDocument doc = XDocument.Parse(framedoc.ToString());
foreach (var node in doc.Descendants("document").ToList())
{
    XNamespace ns = "xsi";
    node.SetAttributeValue(ns + "schema", "");
    node.Name = "alto";
}

and here is the output

<alto p1:schema="" xmlns:p1="xsi">

my goal is like this

xsi:schemaLocation=""

where does the p1 and xmlns:p1="xsi" came from?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
pdf to image
  • 369
  • 6
  • 23
  • 2
    Possible duplicate of [XElement namespaces (How to?)](http://stackoverflow.com/questions/4985974/xelement-namespaces-how-to) – CSharpie Dec 14 '16 at 06:29
  • @CSharpie kindly look at the updated question, why does it gives me wrong output? thank you – pdf to image Dec 14 '16 at 06:49
  • Which bit of that output is "wrong"? Please highlight it in the question, and ideally break the output up into multiple lines to make it a lot easier to read. (If you could simplify this to reduce the bits that are fine, that would help too.) – Jon Skeet Dec 14 '16 at 06:50
  • It's not clear why your code contains "sphinx" when your desired output doesn't have that anywhere in it... – Jon Skeet Dec 14 '16 at 06:51
  • @JonSkeet i updated the question sir. the output is p2:schemaLocation="" and not xsi:schemaLocation="", and about the "sphinx" sorry. i updated it. – pdf to image Dec 14 '16 at 06:52
  • Well you're using `ns + "schemaLocation"` where `ns` refers to `sphinx` for no obvious reason. If you made `ns` have a value of `"http://www.w3.org/2001/XMLSchema-instance"` I suspect it would do what you want. – Jon Skeet Dec 14 '16 at 06:53
  • Possible duplicate of [Use Linq to Xml with Xml namespaces](http://stackoverflow.com/questions/2340411/use-linq-to-xml-with-xml-namespaces) – Alessandro Minneci Dec 14 '16 at 06:59
  • i updated the question, kindly help me whats wrong with my code. thank you – pdf to image Dec 14 '16 at 07:01
  • Do what @JonSkeet suggested. – CSharpie Dec 14 '16 at 07:09
  • And *please* pay attention to code formatting. I fixed it up before, now I need to do it again... look at the code in your question, now look at your code in your IDE. Is your `foreach` statement really indented that far? I strongly suspect it isn't. – Jon Skeet Dec 14 '16 at 07:22

1 Answers1

5

When you write

XNamespace ns = "xsi";

That's creating an XNamespace with a URI of just "xsi". That's not what you want. You want a namespace alias of xsi... with the appropriate URI via an xmlns attribute. So you want:

XDocument doc = XDocument.Parse(framedoc.ToString());
foreach (var node in doc.Descendants("document").ToList())
{
    XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
    node.SetAttributeValue(XNamespace.Xmnls + "xsi", ns.NamespaceName);
    node.SetAttributeValue(ns + "schema", "");
    node.Name = "alto";
}

Or better, just set the alias at the root element:

XDocument doc = XDocument.Parse(framedoc.ToString());
XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
doc.Root.SetAttributeValue(XNamespace.Xmlns + "xsi", ns.NamespaceName);
foreach (var node in doc.Descendants("document").ToList())
{
    node.SetAttributeValue(ns + "schema", "");
    node.Name = "alto";
}

Sample creating a document:

using System;
using System.Xml.Linq;

public class Test
{
    static void Main()
    {
        XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
        XDocument doc = new XDocument(
            new XElement("root",
                new XAttribute(XNamespace.Xmlns + "xsi", ns.NamespaceName),
                new XElement("element1", new XAttribute(ns + "schema", "s1")),
                new XElement("element2", new XAttribute(ns + "schema", "s2"))
            )                         
        );
        Console.WriteLine(doc);
    }
}

Output:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <element1 xsi:schema="s1" />
  <element2 xsi:schema="s2" />
</root>
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194