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;
Related
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>
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")))))))));
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"));
I want to programmatically create a "dcterms:type" element with a namespaced attribute and attribute value using XObjects. The target xml is as follows:
<metadata
xmlns="http://example.org/myapp/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/myapp/ http://example.org/myapp/schema.xsd"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">
...
<dcterms:type xsi:type="dcterms:URI">test</dcterms:type>
...
</metadata>
I have tried:
XNamespace dcterms= XNamespace.Get("http://purl.org/dc/terms/");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace dc = XNamespace.Get("http://purl.org/dc/elements/1.1/");
XAttribute xsiAttribute = new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName);
XAttribute dcAttribute = new XAttribute(XNamespace.Xmlns + "dc", dc.NamespaceName);
XAttribute dcmitypeAttribute = new XAttribute(XNamespace.Xmlns + "dcterms", dcterms.NamespaceName);
...
XElement typeElement = new XElement(dc + "type", "test");
XAttribute typeAttribute = new XAttribute(xsi + "type", dcterms + "URI");
typeElement.Add(typeAttribute);
But this produces:
<dc:type xsi:type="{http://purl.org/dc/terms/}Text">test</dc:type>
... which is wrong and, of course, does not validate. I have also tried hard-coding the value:
XAttribute typeAttribute = new XAttribute(xsi + "type", #"dcterms:URI");
This produces the correct xml, but returns "This is an invalid xsi:type 'http://purl.org/dc/terms/:Text'." when validated via XDocument.Validate().
Ideas?
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)));