Answer the question
In order to leave comments, you need to log in
How to generate prefixed XML using XElement (C#)?
Greetings!
A very simple task, but after spending a whole day looking for an answer, I still did not find a solution. Please help.
I need to generate an XML document like this:
<pref:rootItem xmlns:pref="urn://requiredLink">
<pref:childItem>
...
<pref:mainContent>content</pref:mainContent>
...
</pref:childItem>
</pref:rootItem>
XElement item = new XElement("pref:mainContent", "content");
The ':' character, hexadecimal value 0x3A, cannot be included in a name
XNamespace nameSpace = "urn://requiredLink";
XElement item = new XElement(nameSpace + "mainContent", "content");
Answer the question
In order to leave comments, you need to log in
This is called namespace. https://docs.microsoft.com/en-us/dotnet/standard/l...
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question