Set Root Namespace Prefix in an XDocument [duplicate] - c#

This question already has answers here:
XElement namespaces (How to?)
(2 answers)
Closed 10 years ago.
I currently have:
XNamespace xmlns = "XSDName";<br>
XNamespace xsi = #"http://www.w3.org/2001/XMLSchema-instance";<br>
XNamespace schemaloc = #"XSDName XSDName.xsd";
XDocument xdoc = new XDocument(
new XElement("BaseReport",
new XAttribute(xsi + "schemaLocation", schemaloc),
new XAttribute(XNamespace.Xmlns+"ns1", xmlns),
new XAttribute(XNamespace.Xmlns + "xsi", xsi));
This gives me:
BaseReport xsi:schemaLocation="XSDName XSDName .xsd" xmlns:ns1="XSDName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
How can I have BaseReport read ns1:BaseReport?

The below code will give you the output that you want. The key is adding the defined namespace before the name and letting .NET figure out the correct prefix.
XNamespace xmlns = "XSDName";
XNamespace xsi = #"http://www.w3.org/2001/XMLSchema-instance";
XNamespace schemaloc = #"XSDName XSDName.xsd";
XDocument xdoc = new XDocument(
new XElement(xmlns + "BaseReport",
new XAttribute(xsi + "schemaLocation", schemaloc),
new XAttribute(XNamespace.Xmlns + "ns1", xmlns),
new XAttribute(XNamespace.Xmlns + "xsi", xsi)));

Related

How to add XNamespace (Xml.Linq) to XElement? [duplicate]

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?
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>

Namespace with colon as attribute of xml file in C#

I want to generate a xml code like below example:-
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns ="abc.xyz.xsd" xmlns:xsi="http://namespace">
</Root>
My C# code is like below:
XNamespace xsi = "http://namespace";
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("Root",
new XAttribute("xmlns", "abc.xyz.xsd"),
new XAttribute(XNamespace.Xmlns + "xsi", xsi)
);
This code gives error while saving. What am I doing wrong?
You can use with single namespace
XNamespace xsi = "http://namespace";
XDocument doc1 = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(xsi + "Root",
new XAttribute("xmlns", "abc.xyz.xsd"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://namespace"),
new XElement(xsi + "Child",
new XElement(xsi + "DifferentChild", "other content")
)
));
See this for reference https://msdn.microsoft.com/en-us/library/bb387075.aspx
The output will look like this
<?xml version="1.0" encoding="utf-8"?>
<xsi:Root xmlns="abc.xyz.xsd" xmlns:xsi="http://namespace">
<xsi:Child>
<xsi:DifferentChild>other content</xsi:DifferentChild>
</xsi:Child>
</xsi:Root>
You should add your Root element with namespace (namespace + "Root") since you're using namespaces.
Like this:
XNamespace xsi = "http://namespace";
XNamespace ns = "abc.xyz.xsd";
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(ns + "Root",
new XAttribute("xmlns", ns),
new XAttribute(XNamespace.Xmlns + "xsi", xsi)));
See MSDN article concerning creation of documents having namespaces.

Error: Cannot redefine the namespace for prefix '' used at current element

I am sending a soap request in Xamarin using Linq. I keep getting the error. I know its coming from
this line:
(new XElement ("Subscribe",
new XAttribute ("xmlns", "eAuthService"),
Any help would be greatly appreciated. I have tried everything.
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
XNamespace soap12 = "http://www.w3.org/2003/05/soap-envelope";
XNamespace eauth = "eAuthService";
XName xmlns = "xmlns";
XDocument xDoc = new XDocument (
new XElement (soap12 + "Envelope",
new XAttribute (XNamespace.Xmlns + "xsi", xsi),
new XAttribute (XNamespace.Xmlns + "xsd", xsd),
new XAttribute (XNamespace.Xmlns + "soap12", soap12),
new XElement (soap12 + "Body",
(new XElement ("Subscribe",
new XAttribute ("xmlns", "eAuthService"),
(new XElement ("RequestSubscription",
(new XElement ("Token", "Subscription")),
(new XElement ("ID_Subscription", myResources.registerationId)),
(new XElement ("DateOfBirth", main.pickDate.Text)),
(new XElement ("CountryCode3Letter", "IND")))))))));
It's failing because you're redefining the default namespace (no-prefix) after already writing an element with no prefix (<Subscribe>) under a different namespace (the empty one). You could fix this by doing new XElement(eAuth + "Subscribe"), but the other elements underneath of that wouldn't automatically take on the eAuth namespace, which isn't what I think you intend.
I'm guessing you want to use the eAuth namespace for <Subscribe> as well as all its children--pretty much everything that isn't part of the soap envelope. To do that you need to preface all of your own nodes with eAuth.
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
XNamespace soap12 = "http://www.w3.org/2003/05/soap-envelope";
XNamespace eauth = "eAuthService";
XDocument xDoc = new XDocument (
new XElement (soap12 + "Envelope",
new XAttribute (XNamespace.Xmlns + "xsi", xsi),
new XAttribute (XNamespace.Xmlns + "xsd", xsd),
new XAttribute (XNamespace.Xmlns + "soap12", soap12),
new XElement (soap12 + "Body",
(new XElement (eauth + "Subscribe",
new XAttribute ("xmlns", "eAuthService"),
(new XElement (eauth + "RequestSubscription",
(new XElement (eauth + "Token", "Subscription")),
(new XElement (eauth + "ID_Subscription", myResources.registrationId)),
(new XElement (eauth + "DateOfBirth", main.pickDate.Text)),
(new XElement (eauth + "CountryCode3Letter", "IND")))))))));

Need help creating XML Document with XDocument

I am having trouble creating an XML document using LINQ.
The XML file I need to create has a root element of "ClinicalDocument".
The XML needs to be as follows
<?xml version="1.0" encoding="utf-8"?>
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:hl7-org:v3"
xmlns:voc="urn:hl7-org:v3/voc"
xmlns:sdtc="urn:hl7-org:sdtc">
<!-- Rest of the document here. -->
</ClinicalDocument>
The code I have in trying to do this is
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XDeclaration dec = new XDeclaration("1.0", "UTF-8", null);
XDocument xDoc = new XDocument(dec,null);
XElement cDoc = new XElement("ClinicalDocumnet");
xDoc.Add(cDoc);
cDoc.Add(new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName));
cDoc.Add(new XAttribute("xmlns","urn:hl7-org:v3"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "stdc","urn:hl7-org:sdtc"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "voc","urn:hl7-org:v3/voc"));
xDoc.Save("C:\\test\\test.xml");
I get an exception when trying to save the file.
{"The prefix '' cannot be redefined from '' to 'urn:hl7-org:v3' within the same start element tag."}
XNamespace ns = "urn:hl7-org:v3";
xDoc = new XDocument(dec, null);
cDoc = new XElement(ns + "ClinicalDocument");
xDoc.Add(cDoc);
cDoc.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "stdc","urn:hl7-org:sdtc"));
cDoc.Add(new XAttribute(XNamespace.Xmlns + "voc","urn:hl7-org:v3/voc"));

Generate xml attributes

I have to create an xml file, that contains an element with attributes like:
<element
xsi:schemaLocation="http://test.xsd"
xmlns="http://test2"
xmlns:xsi=http://test3>
I tried:
XNamespace ns = "xsi";
var root = new XElement("element",
new XAttribute(ns + "schemaLocation", "http://test.xsd"), // (I)
new XAttribute(XNamespace.Xmlns, "http://test2"), // (II)
new XAttribute(XNamespace.Xmlns + "xsi", "http://test3"), // (III)
But the only thing that is generated fine is (III):
xmlns:xsi=http://test3
(I) is generated like:
p1:schemaLocation="http://test.xsd" xmlns:p1="xsi"
and (II) is not generated because the line doesn't compile.
Any idea on how I could generate these attributes?
Thank you,
L
EDIT - also found it here: Creating XML with namespaces and schemas from an XElement
const string ns = "http://test2";
const string si = "http://test3";
const string schema_location = "http://test.xsd";
XNamespace xns = ns;
XNamespace sinsp = si;
XElement xe = new XElement(xns + "element",
new XAttribute(XNamespace.Xmlns + "xsi", si),
new XAttribute(sinsp+ "schemaLocation", schema_location),
new XElement(xns + "sometag", "somecontent")
);
return xe;

Categories

Resources