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?

10
  • 2
    Possible duplicate of XElement namespaces (How to?) Commented Dec 14, 2016 at 6:29
  • @CSharpie kindly look at the updated question, why does it gives me wrong output? thank you Commented Dec 14, 2016 at 6: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.) Commented Dec 14, 2016 at 6:50
  • It's not clear why your code contains "sphinx" when your desired output doesn't have that anywhere in it... Commented Dec 14, 2016 at 6:51
  • @JonSkeet i updated the question sir. the output is p2:schemaLocation="" and not xsi:schemaLocation="", and about the "sphinx" sorry. i updated it. Commented Dec 14, 2016 at 6:52

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.