Cannot create XML where the XElement value contains ":" - c#

I have the following code:
private string GetXmlBody()
{
XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace xsdNs = "http://www.w3.org/2001/XMLSchema";
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace outNs = "http://soap.sforce.com/2005/09/outbound";
XNamespace sfNs = "urn:sobject.enterprise.soap.sforce.com";
XDocument requestXml = new XDocument(
new XElement(ns + "Envelope", new XAttribute(XNamespace.Xmlns + "soapenv", ns), new XAttribute(XNamespace.Xmlns + "xsd", xsdNs), new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
new XElement(ns + "Body",
new XElement(outNs + "notifications", new XAttribute("xmlns", outNs),
new XElement(outNs + "OrganizationId", runInfo.OrgId),
new XElement(outNs + "SessionId", runInfo.SessionId),
new XElement(outNs + "EnterpriseUrl", runInfo.Location),
new XElement(outNs + "Notification",
new XElement(outNs + "Id", "04l0H000014TY73QAG"),
new XElement(outNs + "sObject", new XAttribute(XNamespace.Xmlns + "sf", sfNs), new XAttribute(xsiNs + "type", "sf:" + runInfo.RecordType),
new XElement(sfNs + "Id", runInfo.RecordId),
new XElement(sfNs + runInfo.Field, runInfo.FieldValue)
)
)
)
)
)
);
return requestXml.ToString();
}
Which will generate XML needed however I'm running into the following error:
System.Xml.XmlException : The ':' character, hexadecimal value 0x3A, cannot be included in a name.
due to the value of runInfo.FieldValue which contains :. For Example the value may look like:
Opportunity:006i000000sidsh;Account:;userId:a016S00000sjsiq;sandbox:true
So far all the solutions or similar problems that I've seen revolve around producing the correct element name, my problem is around the value. If for instance I remove the : from the runInfo.FieldValue variable then the expected XML is produced.
Any thoughts on how to get around this? I've tried URL encoding the string but that just leads to a similar error except it complains about % values.

Related

Complex Xml namespaces in c#

I am trying to create an xml with multiple namespaces in c#.
I am using LINQ XDocument object to create xml.
The multiple namespaces of xml are causing a lot of confusion.
Can anyone help me out on right direction please.
<?xml version="1.0" encoding="UTF-8"?>
<n1:Form109495CTransmittalUpstream
xmlns="urn:us:gov:treasury:irs:ext:aca:air:6.2" xmlns:irs="urn:us:gov:treasury:irs:common"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:n1="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
xsi:schemaLocation="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd">
<Form1094CUpstreamDetail recordType="String" lineNum="0">
<SubmissionId>1</SubmissionId>
<irs:TaxYr>1000</irs:TaxYr>
<irs:CorrectedInd>true</irs:CorrectedInd>
</Form1094CUpstreamDetail>
</n1:Form109495CTransmittalUpstream>
C# Code
new XDocument(
new XAttribute(XNamespace.Xmlns + "version", "1.0"),
new XAttribute(XNamespace.Xmlns + "encoding", "UTF-8"),
new XElement("n1:Form109495CTransmittalUpstream","")
).Save("sample.xml");
As you may be aware, a 'qualified name' in XML is made up of the namespace and the local name. To make this easier to deal with, you include namespace declarations that prefix these namespaces.
So, for example, where you see irs:TaxYr, the qualified name is actually made up of the urn:us:gov:treasury:irs:common namespace and the TaxYr local name.
The prefix values themselves are pretty unimportant - they're just a lookup mechanism. LINQ to XML will handle this for you automatically (by generating unique prefixes, usually p1, p2 etc.), but you can include them as attributes yourself. To create such a declaration for the irs namespace you'd create the following attribute:
new XAttribute(XNamespace.Xmlns + "irs", "urn:us:gov:treasury:irs:common")
LINQ to XML also provides some nifty implicit conversions, allowing you to create an XNamespace and qualified XName implicitly from a string. So, to get your TaxYr name you'd do the following:
XNamespace irs = "urn:us:gov:treasury:irs:common";
XName taxYr = irs + "TaxYr";
Following this all the way through for each of your elements and attributes, your XML could be created declaratively like so:
XNamespace def = "urn:us:gov:treasury:irs:ext:aca:air:6.2";
XNamespace irs = "urn:us:gov:treasury:irs:common";
XNamespace n1 = "urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var schemaHint = "urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd";
var doc = new XDocument(
new XElement(n1 + "Form109495CTransmittalUpstream",
new XAttribute("xmlns", def),
new XAttribute(XNamespace.Xmlns + "irs", irs),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "n1", n1),
new XAttribute(xsi + "schemaLocation", schemaHint),
new XElement(def + "Form1094CUpstreamDetail",
new XAttribute("recordType", "String"),
new XAttribute("lineNum", 0),
new XElement(def + "SubmissionId", 1),
new XElement(irs + "TaxYr", 1000),
new XElement(irs + "CorrectedInd", true)
)
)
);
This is how I normally do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<n1:Form109495CTransmittalUpstream" +
" xmlns=\"urn:us:gov:treasury:irs:ext:aca:air:6.2\" xmlns:irs=\"urn:us:gov:treasury:irs:common\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:n1=\"urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage\"" +
" xsi:schemaLocation=\"urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd\">" +
"</n1:Form109495CTransmittalUpstream>";
XDocument doc = XDocument.Parse(xml);
XElement form109495CTransmittalUpstream = (XElement)doc.FirstNode;
XNamespace def = form109495CTransmittalUpstream.GetDefaultNamespace();
XNamespace irs = form109495CTransmittalUpstream.GetNamespaceOfPrefix("irs");
XNamespace n1 = form109495CTransmittalUpstream.GetNamespaceOfPrefix("n1");
XElement form1094CUpstreamDetail = new XElement(def + "Form1094CUpstreamDetail", new XAttribute[] {
new XAttribute("recordType", "String"), new XAttribute("lineNum", 0)
});
form109495CTransmittalUpstream.Add(form1094CUpstreamDetail);
form109495CTransmittalUpstream.Add(new XElement("SubmissionId", 1));
form109495CTransmittalUpstream.Add(new XElement(irs + "TaxYr", 1000));
form109495CTransmittalUpstream.Add(new XElement(irs + "CorrectedInd", true));
}
}
}
​

C# with XDocument and xsi:schemaLocation

I want to create the following XML.
<?xml version="1.0" encoding="utf-8"?>
<MyNode xsi:schemaLocation="https://MyScheme.com/v1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://MyScheme.com/v1-0 Sscheme.xsd">
<MyInfo>
<MyNumber>string1</MyNumber>
<MyName>string2</MyName>
<MyAddress>string3</MyAddress>
</MyInfo>
<MyInfo2>
<FirstName>string4</FirstName>
</MyInfo2>
</MyNode>
I'm using this code.
XNamespace xmlns = "https://MyScheme.com/v1-0 Sscheme.xsd";
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace schemaLocation = XNamespace.Get("https://MyScheme.com/v1-0");
XDocument xmlDocument = new XDocument(
new XElement(xmlns + "MyNode",
new XAttribute(xsi + "schemaLocation", schemaLocation),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XElement("MyInfo",
new XElement("MyNumber", "string1"),
new XElement("MyName", "string2"),
new XElement("MyAddress", "string3")
),
new XElement("MyInfo2",
new XElement("FirstName", "string4")
)
)
);
xmlDocument.Save("C:\\MyXml.xml");
However, I'm getting xmlns="" inside tags MyInfo and MyInfo2.
Can somebody help me to create the correct XML?
You need to use xmlns XNamespace for all elements, because that is default namespace and it is declared at root level element. Note that descendant elements inherit ancestor default namespace implicitly, unless otherwise specified :
XDocument xmlDocument = new XDocument(
new XElement(xmlns + "MyNode",
new XAttribute(xsi + "schemaLocation", schemaLocation),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XElement(xmlns+"MyInfo",
new XElement(xmlns+"MyNumber", "string1"),
new XElement(xmlns+"MyName", "string2"),
new XElement(xmlns+"MyAddress", "string3")
),
new XElement(xmlns+"MyInfo2",
new XElement(xmlns+"FirstName", "string4")
)
)
);
Dotnetfiddle Demo

How do I create XML with namespace prefixes?

How should I write code in C# to create xml file like this
<ymaps:GeoObjectCollection>
<gml:name>Something</gml:name>
<gml:featureMembers>
<ymaps:GeoObject>
<gml:name>Something</gml:name>
<gml:description>Something</gml:description>
<gml:LineString>
<gml:pos>50.588298 55.145683</gml:pos>
<gml:pos>50.588290 55.145678</gml:pos>
<gml:pos>50.588288 55.145678</gml:pos>
</gml:LineString>
<ymaps:style>#customStyle1</ymaps:style>
</ymaps:GeoObject>
this yandex's xml file, and it has xsd file on http://maps.yandex.ru/schemas/ymaps/1.x/ymaps.xsd
First familiarize yourself with XML, XML Namespaces, XML Schemas and how to process XML data in .NET. Once you have mastered the basics, use the XML Schema Definition Tool (Xsd.exe) to generate classes from the XSD and use it from within your application.
Here is a demonstration of how to create the desired XML using LINQ to XML. I have created two classes to store the data: GeoObject and Pos:
var geoObjects = new[] {
new GeoObject {
Name = "Something",
Description = "Something",
Line = new[] {
new Pos { X = 50.588298M, Y = 55.145683M },
new Pos { X = 50.588290M, Y = 55.145678M },
new Pos { X = 50.588288M, Y = 55.145678M }
},
Style = "#customStyle1"
}
};
The XML is created using this code:
XNamespace ymaps = "http://maps.yandex.ru/ymaps/1.x";
XNamespace gml = "http://www.opengis.net/gml";
var xElement = new XElement(
ymaps + "GeoObjectCollection",
new XAttribute(XNamespace.Xmlns + "ymaps", ymaps),
new XAttribute(XNamespace.Xmlns + "gml", gml),
new XElement(gml + "name", "Something"),
new XElement(gml + "featureMembers",
geoObjects.Select(
geoObject => new XElement(
ymaps + "GeoObject",
new XElement(gml + "name", geoObject.Name),
new XElement(gml + "description", geoObject.Description),
new XElement(gml + "LineString",
geoObject.Line.Select(
pos => new XElement(
gml + "pos",
String.Format(CultureInfo.InvariantCulture, "{0} {1}", pos.X, pos.Y)
)
)
),
new XElement(ymaps + "style", geoObject.Style)
)
)
)
);
If you write out xElement you get the following XML:
<ymaps:GeoObjectCollection xmlns:ymaps="http://maps.yandex.ru/ymaps/1.x" xmlns:gml="http://www.opengis.net/gml">
<gml:name>Something</gml:name>
<gml:featureMembers>
<ymaps:GeoObject>
<gml:name>Something</gml:name>
<gml:description>Something</gml:description>
<gml:LineString>
<gml:pos>50.588298 55.145683</gml:pos>
<gml:pos>50.588290 55.145678</gml:pos>
<gml:pos>50.588288 55.145678</gml:pos>
</gml:LineString>
<ymaps:style>#customStyle1</ymaps:style>
</ymaps:GeoObject>
</gml:featureMembers>
</ymaps:GeoObjectCollection>

C# XML - Why does this Code keep failing with 0x3A Error?

i have following code example:
PlentySoapRequest_GetAuthentificationToken username = new PlentySoapRequest_GetAuthentificationToken();
username.Username = user_textbox.ToString();
username.Userpass = password_textbox.ToString();
Uri uri = new Uri("http://www.****.de/plenty/api/soap/version105/");
XNamespace soapenv = #"http://schemas.xmlsoap.org/soap/envelope/";
XNamespace xsi = #"http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = #"http://www.w3.org/2001/XMLSchema";
XNamespace ver = #"http://www.****.de/plenty/api/soap/version105/";
var document = new XDocument(
new XDeclaration("1.0", "utf-8", String.Empty),
new XElement(soapenv + "Envelope",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "xsd" , xsd),
new XAttribute(XNamespace.Xmlns + "soapenv" , soapenv),
new XAttribute(XNamespace.Xmlns + "ver" , ver),
new XElement(soapenv + "Header"),
new XElement(soapenv + "Body",
new XElement(ver + "GetAuthentificationToken",
new XElement("oLogin" + xsi + "type" + ver + "PlentySoapRequest_GetAuthentificationToken",
new XAttribute("Username" + xsi + "type" + xsd + "string", username.Username),
new XAttribute("Userpass" + xsi + "type" + xsd + "string", username.Userpass)
)
)
)
)
);
I keep getting the error message in the first line. "plentysoaprequest...."
The ':' character, hexadecimal value 0x3A, cannot be included in a name.
if I comment out those lines, it keeps say it is in the first line of the code.
Edit:
the xml should look like this:
<soapenv:Body>
<ver:GetAuthentificationToken>
<oLogin xsi:type="ver:PlentySoapRequest_GetAuthentificationToken">
<!--You may enter the following 2 items in any order-->
<Username xsi:type="xsd:string">apitest</Username>
<Userpass xsi:type="xsd:string">apitest</Userpass>
</oLogin>
</ver:GetAuthentificationToken>
so there seems to be a problem with the xml-sysntax.
I cant figure out how to set xsi:type or xsi:type
"oLogin" + xsi + "type" will create a string with value "oLoginhttp://www.w3.org/2001/XMLSchema-instancetype". That's not a valid name...
You need something a little closer to this:
var document = new XDocument(
new XDeclaration("1.0", "utf-8", String.Empty),
new XElement(soapenv + "Envelope",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "xsd", xsd),
new XAttribute(XNamespace.Xmlns + "soapenv", soapenv),
new XAttribute(XNamespace.Xmlns + "ver", ver),
new XElement(soapenv + "Header"),
new XElement(soapenv + "Body",
new XElement(ver + "GetAuthentificationToken",
new XElement(xsi + "Login",
new XAttribute(xsi + "type", "blahblah"),
new XElement("Username",
new XAttribute(xsi + "type", "xsd:string"),
"myUserName")
)
)
)
)
);
Which generates this XML
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ver="http://www.****.de/plenty/api/soap/version105/">
<soapenv:Header />
<soapenv:Body>
<ver:GetAuthentificationToken>
<xsi:Login xsi:type="blahblah">
<Username xsi:type="xsd:string">myUserName</Username>
</xsi:Login>
</ver:GetAuthentificationToken>
</soapenv:Body>
</soapenv:Envelope>

How to generate xsi:schemalocation attribute correctly when generating a dynamic sitemap.xml with LINQ to XML?

I am generating a dynamic sitemap.xml
According to sitemaps.org this is the header for a sitemap.xml
<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
...
</url>
</urlset>
So I'm using LINQ To XML to generate the sitemap.xml
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
return new XElement(ns + "urlset",
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
//new XAttribute("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
from node in new GetNodes()
select new XElement(ns + "url",
new XElement(ns + "loc", node.Loc),
new XElement(ns + "lastmod", node.LastMod),
new XElement(ns + "priority", node.Priority)
)
).ToString();
The commented line is the one i cannot get right.
How can i set the "xsi:schemalocation" attribute?
Thanks.
Ok, I got it right. Thanks to Mike Caron
If I declare the XAtrribute(XNamespace.Xmlns + "xsi",...) then it works
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
return new XElement(ns + "urlset",
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
from node in GetNodes()
select new XElement(ns + "url",
new XElement(ns + "loc", node.Loc),
new XElement(ns + "lastmod", node.LastMod),
new XElement(ns + "priority", node.Priority)
)
).ToString();
I don't know LINQ to XML, but after a quick peek at the documentation, try this:
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
return new XElement(ns + "urlset",
new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
from node in new GetNodes()
select new XElement(ns + "url",
new XElement(ns + "loc", node.Loc),
new XElement(ns + "lastmod", node.LastMod),
new XElement(ns + "priority", node.Priority)
)
).ToString();
Note that I'm not setting the xmlns attributes explicitly. I suspect they're generated automatically. Also, caveat emptor, since this is not tested.

Categories