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>
Related
So, I try creating Xml document with next code:
XNamespace spr1 = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace ex = "urn:schemas-microsoft-com:office:excel";
XNamespace spr2 = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace rec = "http://www.w3.org/TR/REC-html40";
var xworkbook = new XElement(spr1 + "Workbook");
xworkbook.Add(new XAttribute(XNamespace.Xmlns + "x", ex));
xworkbook.Add(new XAttribute(XNamespace.Xmlns +"ss", spr2));
xworkbook.Add(new XAttribute(XNamespace.Xmlns + "html", rec));
This code make next xml:
<ss:Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<!--Xml body-->
</ss:Workbook>
But I expect this:
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
</Workbook>
How to build Workbook element without "ss" prefix and with needed "xmlns" attribute?
LINQ to XML uses the namespace prefix that is closest as it looks through all the attributes in reverse order from the current element to the root. So if you add the default namespace explicitly at the end, then Workbook will use that and not the ss prefix.
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace ex = "urn:schemas-microsoft-com:office:excel";
XNamespace html = "http://www.w3.org/TR/REC-html40";
var workbook = new XElement(
ss + "Workbook",
new XAttribute(XNamespace.Xmlns + "x", ex),
new XAttribute(XNamespace.Xmlns + "ss", ss),
new XAttribute(XNamespace.Xmlns + "html", html),
new XAttribute("xmlns", ss)
);
This gives you the XML below:
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns="urn:schemas-microsoft-com:office:spreadsheet" />
As stated in the comments, the two documents in your question are semantically the same. Any XML parser shouldn't care about the difference between the two documents.
I usually do it like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"" +
" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"" +
" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"" +
" xmlns:html=\"http://www.w3.org/TR/REC-html40\">" +
"</Workbook>";
XDocument doc = XDocument.Parse(xml);
XElement workbook = (XElement)doc.FirstNode;
XNamespace ssNs = workbook.GetNamespaceOfPrefix("ss");
XElement worksheet = new XElement(ssNs + "Worksheet");
workbook.Add(worksheet);
}
}
}
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.
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")))))))));
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;