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>
Related
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.
Here I am trying to format XML from a list and I am not getting the proper format. Here is my code:
protected void GenerateXml(string url, List<string> listitems) //generateXml
{
XNamespace nsXhtml = "http://www.w3.org/1999/xhtml";
XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace nsImage = "http://www.google.com/schemas/sitemap-image/1.1";
var sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", ""));
var urlSet =
new XElement(
nsSitemap + "urlset",
new XAttribute("xmlns", nsSitemap),
new XAttribute(XNamespace.Xmlns + "image", nsXhtml),
from urlNode in listitems
select
new XElement(
nsSitemap + "url",
new XElement(nsSitemap + "loc", url),
new XElement(nsSitemap + "image",
new XElement(nsSitemap + "imageloc", urlNode))));
sitemap.Add(urlSet);
sitemap.Save(System.Web.HttpContext.Current.Server.MapPath("/Static/sitemaps/Sitemap-image.xml"));
}
... and getting the format like this:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.w3.org/1999/xhtml">
<url>
<loc>http://example.com/intl/cars/new-models/the-new-s90</loc>
<image>
<imageloc>http://example.com/static/images/volvo-logo-scaled.png</imageloc>
</image>
</url>
<url>
<loc>http://example.com/intl/cars/new-models/the-new-s90</loc>
<image>
<imageloc>http://assets.example.com/intl/~/media/images/galleries/new-cars/packshots/small/allnew_xc90-side_2.png</imageloc>
</image>
</url>
</urlset>
But I need in this format:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://example.com/sample.html</loc>
<image:image>
<image:loc>http://example.com/image.jpg</image:loc>
</image:image>
<image:image>
<image:loc>http://example.com/photo.jpg</image:loc>
</image:image>
</url>
</urlset>
Any suggestion?
In addition to properly handling the multiple nested elements, you never assigned the image prefix to the elements that are supposed to have them, you continue to use the global namespace:
new XElement(nsSitemap + "image",
new XElement(nsSitemap + "imageloc", urlNode)
nsSitemap should be nsImage, and "imageloc" should be "loc".
A couple minor tweaks to your code will get you what you're looking for:
protected void GenerateXml(string url, List<string> listitems) //generateXml
{
XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace nsImage = "http://www.google.com/schemas/sitemap-image/1.1";
var sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", ""));
var urlSet = new XElement(nsSitemap + "urlset",
new XAttribute("xmlns", nsSitemap),
new XAttribute(XNamespace.Xmlns + "image", nsImage),
new XElement(nsSitemap + "url",
new XElement(nsSitemap + "loc", url),
from urlNode in listitems
select new XElement(nsImage + "image",
new XElement(nsImage + "loc", urlNode)
)));
sitemap.Add(urlSet); sitemap.Save(System.Web.HttpContext.Current.Server.MapPath("/Static/sitemaps/Sitemap-image.xml"));
}
Notice the following changes:
new XAttribute(XNamespace.Xmlns + "image", nsImage);
This sets the namespace correctly to mach your expected output.
new XElement(nsImage + "image",
new XElement(nsImage + "loc", urlNode)
This sets the image prefix correctly.
Notice how "loc" and "url" were moved to before the from query.
The above code results in the following output XML:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://example.com/sample.html</loc>
<image:image>
<image:loc>http://example.com/image.jpg</image:loc>
</image:image>
<image:image>
<image:loc>http://example.com/photo.jpg</image:loc>
</image:image>
</url>
</urlset>
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
I am trying to create a KML file dynamically in C#. I have wrote a recursive function to do this. However the result of output has a bit problem. The problem is the position of closing tags of all placemarks. I am really confused. Please tell me where am I making mistake in the recursive function???
My code:
private void xmlBuild()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XComment("This is comment by me"),
new XElement(ns+"kml",
new XElement(ns+"Document", rec_build())));
doc.Save(Server.MapPath(#"~\App_Data\markers2.xml"));
}
private XElement rec_build()
{
if (iteration != 0)
{
iteration -= 1;
return final_rec = new XElement(ns + "Placemark",
new XAttribute("id", "1"),
new XElement(ns + "title", "something"),
new XElement(ns + "description", "something"),
new XElement(ns + "LookAt",
new XElement(ns + "Longitude", "49.69"),
new XElement(ns + "Latitude", "32.345")), new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")),rec_build());
}
else
{
return null;
}
}
and this is the output for iteration value of 2: (please notice the closing tags of placemark id=1 at the end of file. It should be before the starting tag of placemark id=2!
<?xml version="1.0" encoding="utf-8"?>
<!--This is comment by me-->
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark id="1">
<title>something</title>
<description>something</description>
<LookAt>
<Longitude>49.69</Longitude>
<Latitude>32.345</Latitude>
</LookAt>
<Point>
<coordinates>49.69,32.345,0</coordinates>
</Point>
<Placemark id="1">
<title>something</title>
<description>something</description>
<LookAt>
<Longitude>49.69</Longitude>
<Latitude>32.345</Latitude>
</LookAt>
<Point>
<coordinates>49.69,32.345,0</coordinates>
</Point>
</Placemark>
</Placemark>
</Document>
</kml>
So the problem is each time you recurse, you are adding the element to the newly created item. It seems that a loop would work better.
Essentially code is doing this:
set up the kml outbody
1st call and add element (element 1) to kml outerboy
2nd call add element (element 2) to (element 1)
3rd call add element (element 3) to (element 2).
If you wanted to do a recursive method rather then the looping mechanism, pass in a reference to the outer kml .
The recursive is more confusing if this is exactly how it works
(Sorry if I have an extra or missing parenthesis, comma, or other item. I don't have VS installed on this)
Looping:
private void xmlBuild()
{
XElement documentElement = new XElement(ns + "Document");
for (int i = 0; i < 2; i++)
{
documentElement.Add(rec_build());
}
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XComment("This is comment by me"),
new XElement(ns + "kml", documentElement));
doc.Save(Server.MapPath(#"~\App_Data\markers2.xml"));
}
private XElement rec_build()
{
return new XElement(ns + "Placemark",
new XAttribute("id", "1"),
new XElement(ns + "title", "something"),
new XElement(ns + "description", "something"),
new XElement(ns + "LookAt",
new XElement(ns + "Longitude", "49.69"),
new XElement(ns + "Latitude", "32.345")),
new XElement(ns + "Point",
new XElement(ns + "coordinates", "49.69,32.345,0")));
}
Recursive:
private void xmlBuild()
{
XElement docElement = new XElement(ns+"Document");
rec_build(docElement);
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XComment("This is comment by me"),
new XElement(ns+"kml", docElement)));
doc.Save(Server.MapPath(#"~\App_Data\markers2.xml"));
}
private XElement rec_build(XElement doc)
{
if (iteration != 0)
{
iteration -= 1;
doc.Add(new XElement(ns + "Placemark",
new XAttribute("id", "1"),
new XElement(ns + "title", "something"),
new XElement(ns + "description", "something"),
new XElement(ns + "LookAt",
new XElement(ns + "Longitude", "49.69"),
new XElement(ns + "Latitude", "32.345")),
new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")));
return recBuild(doc);
}
else
{
return null;
}
}
You are adding the recursively build elements as children of Placemark and not Document. This should do the trick:
private void xmlBuild()
{
XElement docElement = new XElement(ns + "Document");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XComment("This is comment by me"),
new XElement(ns + "kml", docElement));
rec_build(docElement);
doc.Save(Server.MapPath(#"~\App_Data\markers2.xml"));
}
private XElement rec_build(XElement docElement)
{
if (iteration != 0)
{
iteration -= 1;
return final_rec = new XElement(ns + "Placemark",
new XAttribute("id", "1"),
new XElement(ns + "title", "something"),
new XElement(ns + "description", "something"),
new XElement(ns + "LookAt",
new XElement(ns + "Longitude", "49.69"),
new XElement(ns + "Latitude", "32.345")),
new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")));
docElement.Add(final_rec);
rec_build(docElement);
}
else
return null;
}
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.