I'm developing an ASP.NET MVC application with .NET Framework 4.5.1 that returns XML generated from database data.
I want to obtain this:
<?xml version="1.0" encoding="utf-8"?>
<pmlcore:Sensor [ Ommitted for brevety ] ">
But I get this:
<?xml version="1.0" encoding="utf-8"?>
<Sensor [ Ommitted for brevety ] xmlns="pmlcore">
Reading all the answers found in Stackoverflow, I changed my code to use XNamespace:
XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
XDeclaration dec = new XDeclaration("1.0", "utf-8", null);
XNamespace pmlcore = "pmlcore";
XNamespace pmluid = "pmluid";
root = new XElement(pmlcore + "Sensor"
, new XAttribute(XNamespace.Xmlns + "pmluid",
"urn:autoid:specification:universal:Identifier:xml:schema:1")
, new XAttribute(XNamespace.Xmlns + "xsi", ns)
, new XAttribute(XNamespace.Xmlns + "pmlcore",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1")
, new XAttribute(ns + "noNamespaceSchemaLocation",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1 ./PML/SchemaFiles/Interchange/PMLCore.xsd")
How can I obtain <pmlcore:Sensor this?
If I use this code:
root = new XElement("pmlcore:Sensor"
I get this error:
The ':' character, hexadecimal value 0x3A, cannot be included in a
name
The problem is that you're adding the wrong namespace... you're trying to use the alias for it, instead of the namespace URI. Here's a concrete example that works:
using System;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
XNamespace pmlcore = "urn:autoid:specification:interchange:PMLCore:xml:schema:1";
XNamespace pmluid = "urn:autoid:specification:universal:Identifier:xml:schema:1";
var root = new XElement(pmlcore + "Sensor",
new XAttribute(XNamespace.Xmlns + "pmluid", pmluid.NamespaceName),
new XAttribute(XNamespace.Xmlns + "pmlcore", pmlcore.NamespaceName));
Console.WriteLine(root);
}
}
Output (reformatted):
<pmlcore:Sensor
xmlns:pmluid="urn:autoid:specification:universal:Identifier:xml:schema:1"
xmlns:pmlcore="urn:autoid:specification:interchange:PMLCore:xml:schema:1" />
Related
I try programming the following xml document (simplified) as code:
<P xmlns="http://schemas.microsoft.com/x/2010/manifest"
xmlns:ab="http://schemas.microsoft.com/a/2010/manifest"
xmlns:ac="http://schemas.microsoft.com/a/2013/manifest">
<ab:Ex xmlns:ab="http://schemas.microsoft.com/a/2013/manifest">
</ab:Ex>
</P>
Now an element redefines the namespace:
xmlns:ab="http://schemas.microsoft.com/a/2013/manifest".
how do I get the element into my program? It is obviously valid because I can load the xml file like this:
Add(new XAttribute (XNamespace.Xmlns + "ab")
I get the error message:
System.Xml.XmlException: 'The prefix 'ab' cannot be redefined from 'http://schemas.microsoft.com/a/2010/manifest' to 'http://schemas.microsoft.com/a/2013/manifest' within the same start element tag.'
Actually logical (redefinition) but when I load the document, the element is accepted.
using System.Xml.Linq;
namespace xmltest
{
class Program
{
static void Main(string[] args)
{
//Load
XDocument doc1 = XDocument.Load(#"c:\simple.xml", LoadOptions.None);
System.Console.WriteLine(doc1.ToString());
//Create new
XNamespace ab = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace nsx = "http://schemas.microsoft.com/x/2010/manifest";
XDocument doc2 = new XDocument(new XDeclaration("1.0", "utf-8", ""),
new XElement(nsx + "P"));
doc2.Element(nsx + "P").Add(new XAttribute("xmlns",
"http://schemas.microsoft.com/x/2010/manifest"));
doc2.Element(nsx + "P").Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2010/manifest"));
doc2.Element(nsx + "P").Add(new XAttribute(XNamespace.Xmlns + "ac",
"http://schemas.microsoft.com/a/2013/manifest"));
XElement xml = new XElement(ab + "Ex");
//--> Below does not work work
xml.Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2013/manifest"));
doc2.Element(nsx + "P").Add(xml);
System.Console.WriteLine(doc2.ToString());
}
}
}
Anybody have any idea how to solve this problem?
In theory, because
<ab:Ex xmlns:ab="http://schemas.microsoft.com/a/2013/manifest">
actually means that Ex is to be defined in the new namespace (2013), you can get what you want by defining Ex in the new namespace, i.e. something like:
XNamespace abOld = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace abNew = "http://schemas.microsoft.com/a/2013/manifest";
XNamespace nsx = ...
XElement xml = new XElement(abNew + "Ex");
xml.Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2013/manifest"));
However, repurposing the alias of an existing namespace just so that you can use the same alias is going to be really confusing to future maintenance - as the alias leaves scope, it will revert to it's old definition. I would recommend that you at least create different, unique aliases for the 2010 and 2013 namespaces.
Also, unless the intended consumer of the Xml was buggy, and required specific aliases or namespace scopes, I would generally avoid micro-managing the namespace aliasing and namespace scoping - let Linq to Xml manage that for you.
For instance, how about just:
XNamespace abOld = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace abNew = "http://schemas.microsoft.com/a/2013/manifest";
// Build up the document by providing element, attribute + applicable namespaces:
var root = new XElement(abOld + "P",
new XElement(abNew + "Ex"));
I am creating xml in C#, and want to add Namespace and Declaration . My xml as below:
XNamespace ns = "http://ab.com//abc";
XDocument myXML = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XElement(ns + "Root",
new XElement("abc","1")))
This will add xmlns="" at both Root level and child element abc level as well.
<Root xmlns="http://ab.com/ab">
<abc xmlns=""></abc>
</Root>
But I want it in only Root level not in child level like below:
<Root xmlns="http://ab.com/ab">
<abc></abc>
</Root>
and how to add Declaration at Top, my code not showing Declaration after run.
Please help me to get the complete xml as
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root xmlns="http://ab.com/ab">
<abc></abc>
</Root>
You need to use the same namespace in your child elements:
XDocument myXML = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XElement(ns + "Root",
new XElement(ns + "abc", "1")))
If you just use "abc" this will be converted to an XName with no Namespace. This then results in an xmlns="" attribute being added so the fully qualified element name for abc would be resolved as such.
By setting the name to ns + "abc" no xmlns attribute will be added when converted to string as the default namespace of http://ab.com/ab is inherited from Root.
If you wanted to simply 'inherit' the namespace then you wouldn't be able to do this in such a fluent way. You'd have create the XName using the namespace of the parent element, for example:
var root = new XElement(ns + "Root");
root.Add(new XElement(root.Name.Namespace + "abc", "1"));
Regarding the declaration, XDocument doesn't include this when calling ToString. It will if you use Save to write to a Stream, TextWriter, or if you supply an XmlWriter that doesn't have OmitXmlDeclaration = true in its XmlWriterSettings.
If you wanted to just get the string, this question has an answer with a nifty extension method using a StringWriter.
Use the namespace on all elements you create:
XDocument myXML = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XElement(ns + "Root",
new XElement(ns + "abc","1")))
I am reading from a csv file and transferring the data to an xml file using XmlSerializer in c#.But now I am facing a problem with the namespaces in the root element.My required xml should be in the following format.
<?xml version="1.0" encoding="ASCII"?>
<abc:Country xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:abc="some url">
<Person></Person>
</abc:Country>
But I am getting my output in this format:
<?xml version="1.0" encoding="ASCII"?>
<Country xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person></Person>
</Country>
I want the namespace of abc to be included in the root and then also "abc" should prefix only my root element i.e "Country". I tried using various options mentioned online but none of them actually worked for me.When i use the XmlSerializerNamespaces and overload my Serialiser class all opther namespaces disappear.So could you let me know how I could achieve this.
Is it important to use XmlSerializer? This sort of thing is pretty easy to do with XDocument instead. Something like this:
var document = new XDocument();
XNamespace abcns = "http://some/url/abc";
XNamespace xmins = "http://www.omg.org/XMI";
XNamespace xsins = "http://www.w3.org/2001/XMLSchema-instance";
var element = new XElement(abcns + "Country",
new XAttribute(XNamespace.Xmlns + "abc", abcns),
new XAttribute(XNamespace.Xmlns + "xmi", xmins),
new XAttribute(XNamespace.Xmlns + "xsi", xsins),
new XAttribute(xmins + "version", "2.0"),
new XElement("Person"));
document.Add(element);
We can use the following to include multiple namespaces in the root element of the xml:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xmi", "http://www.omg.org/XMI");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("abc", "some url");
XmlSerializer serializer = new XmlSerializer(typeof(Country));
TextWriter textWriter = new StreamWriter(#"C:\test.xml", true, Encoding.ASCII);
serializer.Serialize(textWriter, country, ns);
"country" would be the object that you would be creating for the class "Country"(root element of your xml).
I have XML like this:
<stream:stream to="lap-020.abcd.co.in" from="sourav#lap-020.abcd.co.in" xml:lang="en" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"/>
Try to generate the XML using XDocument like this
private readonly XNamespace _streamNamespace = "http://etherx.jabber.org/streams";
private readonly XName _stream;
_stream = _streamNamespace + "stream";
XDocument xdoc=new XDocument(
new XElement(_stream,
new XAttribute("from", "sourav#lap-020.abcd.co.in"),
new XAttribute("to","lap-020.abcd.co.in"),
new XAttribute("xmlns:stream","http://etherx.jabber.org/streams"),
new XAttribute("version","1.0"),
new XAttribute("xml:lang","en")
));
But I get an exception:
Additional information: The ':' character, hexadecimal value 0x3A, cannot be included in a name.
To add namespace declaration you can use XNamespace.Xmlns, and to reference the predefined namespace prefix xml use XNamespace.Xml, for example :
XNamespace stream = "http://etherx.jabber.org/streams";
var result = new XElement(stream + "stream",
new XAttribute("from", "sourav#lap-020.abcd.co.in"),
new XAttribute("to","lap-020.abcd.co.in"),
new XAttribute(XNamespace.Xmlns + "stream", stream),
new XAttribute("version","1.0"),
new XAttribute(XNamespace.Xml+"lang","en"),
String.Empty);
Console.WriteLine(result);
//above prints :
//<stream:stream from="sourav#lap-020.abcd.co.in" to="lap-020.abcd.co.in"
// xmlns:stream="http://etherx.jabber.org/streams" version="1.0"
// xml:lang="en">
//</stream:stream>
you can add the namespace like
XElement root = new XElement("{http://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement("{http://www.adventure-works.com}Child", "child content")
);
This example produces the following output:
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
I need to add multiple namespaces to the xdocument with same address (C#)
<root xmlns:f="urn://xml.voodoo.net/vd/formating-1.0" xmlns="urn://xml.voodoo.net/vd/formating-1.0">
<a:something>stuff and more stuff</a:something>
</root>
if i add using the below code.. it shows only xmlns:f
XNamespace defaultNS = "urn://xml.voodoo.net/vd/formating-1.0";
XNamespace f = "urn://xml.voodoo.net/vd/formating-1.0";
XElement rootElement = new XElement(defaultNS + "root",
new XAttribute(XNamespace.Xmlns + "f", f.NamespaceName),
how to show 2 namespaces?? is it even possible?
var doc = new XDocument(
new XElement(defaultNS + "root",
new XAttribute(XNamespace.Xmlns + "f", defaultNS),
new XAttribute("xmlns", defaultNS),
new XElement(defaultNS + "something",
new XAttribute(XNamespace.Xmlns + "f", defaultNS), "stuff and more stuff")
)
);
Desired output:
<root xmlns:f="urn://xml.voodoo.net/vd/formating-1.0" xmlns="urn://xml.voodoo.net/vd/formating-1.0">
<f:something xmlns:f="urn://xml.voodoo.net/vd/formating-1.0">stuff and more stuff</f:something>
</root>
Your XML example is incorrect, the 'f' namespace is not used, whereas there is an 'a' namespace present.
Your C# code does not match your XML, it creates an element with an attribute.
Anyhow, a namespace definitions within an XML document only makes sense if you actually use it. If you create an XML document via C# code, it will generate XML which is semantically correct, yet might not match the syntax of your example.