I'm trying to set x:Key in some XAML using Linq to XML so I can add a value converter to the resource dictionary of a data template:
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace local = "clr-namespace:App,assembly=App";
XElement dt = new XElement(xmlns + "DataTemplate",
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute(XNamespace.Xmlns + "local", "clr-namespace:App,assembly=App"),
new XElement(xmlns + "DataTemplate.Resources",
new XElement(local + "MyConverter",
new XAttribute("x:Key", "myConverter"))));
However this raises an exception complaining that ':' is not allowed in attribute names. Using another XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml" and writing x + "Key" doesn't work either - it gives p3:Key.
Is there some way to include a colon in XAttribute names?
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace local = "clr-namespace:App,assembly=App";
XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
XElement dt = new XElement(xmlns + "DataTemplate",
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute(XNamespace.Xmlns + "local", "clr-namespace:App,assembly=App"),
new XElement(xmlns + "DataTemplate.Resources",
new XElement(local + "MyConverter",
new XAttribute(x + "Key", "myConverter"))));
Console.WriteLine(dt);
Output:
<DataTemplate xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:App,assembly=App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<DataTemplate.Resources>
<local:MyConverter x:Key="myConverter" />
</DataTemplate.Resources>
</DataTemplate>
Related
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)));
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;