I am trying to programmatically add a new XLink node to an XDocument but .Net seems to be creating them as atomized namespaces and names and I can't find any code to add XLink nodes to XML?
My code looks like this:
//read in the current XML content
XDocument content = XDocument.Parse(xmlContent);
//add a new node called large images
XElement newNode = new XElement("large_images", "");
newNode.SetAttributeValue("{xmlns}xlink", "http://www.w3.org/1999/xlink");
newNode.SetAttributeValue("{xlink}type", "simple");
newNode.SetAttributeValue("{xlink}href", "tcm:5-550");
newNode.SetAttributeValue("{xlink}title", "of1_454x340.jpg");
content.Add(newNode);
Unfortunately this newNode comes out like this:
<large_images p1:xlink="http://www.w3.org/1999/xlink" p2:type="simple" p2:href="tcm:5-550" p2:title="of1_454x340.jpg" xmlns:p2="xlink" xmlns:p1="xmlns"></large_images>
But I need the node to look like this in order for it to pass the XML validation:
<large_images xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-550" xlink:title="of1_454x340.jpg"></large_images>
Can anybody help? I don't want to go down the String.Replace() route as it seems that this must be possible another way?
Thanks
Ryan
I'd do that as:
XNamespace ns = "http://www.w3.org/1999/xlink";
XElement newNode = new XElement("large_images",
new XAttribute(XNamespace.Xmlns + "xlink", ns),
new XAttribute(ns + "type", "simple),
new XAttribute(ns + "href", "tcm:5-550"),
new XAttribute(ns + "title", "of1_454x340.jpg"));
This produces XML of:
<large_images xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
xlink:href="tcm:5-550" xlink:title="of1_454x340.jpg" />
Related
Below is my controller Code which generate the XML file and save this file and specific location
List<propertyfile> data = ws.GetPropertyFiles();
XmlDocument writer = new XmlDocument();
// Create XML declaration
XmlNode declaration = writer.CreateNode(XmlNodeType.XmlDeclaration, null, null);
writer.AppendChild(declaration);
// Make the root element first
XmlElement root = writer.CreateElement("Channel");
writer.AppendChild(root);
//XmlElement rot = writer.CreateElement("item");
//writer.AppendChild(rot);
//Server.MapPath //HttpContext.Current.Server.MapPath("~")
string wallFolderPath = Server.MapPath("~/assets/wall/");
var newGuid = Guid.NewGuid().ToString();
string filename = wallFolderPath+ "wallview_" +newGuid+".xml";
var pathlocal = ServerUrlPath+"assets/wall/" + "wallview_" + newGuid + ".xml";
ViewBag.wallpath = pathlocal;
System.IO.File.Create(filename).Dispose();
foreach (var a in data)
{
if (a.Path != null && a.ThumbnailPath != null)
{
XmlElement id = writer.CreateElement("item");
XmlElement name = writer.CreateElement("title");
name.InnerText = a.Name;
XmlElement age = writer.CreateElement("media:description");
var e_path = a.Path;
XmlElement anchor = writer.CreateElement("a");
e_path = e_path.Substring(2, e_path.Length - 2);
e_path= ServerUrlPath + e_path;
anchor.SetAttribute("href", e_path);
age.AppendChild(anchor);
//age.SetAttribute("href", e_path);
XmlElement linq = writer.CreateElement("link");
var e_linq = a.Path;
e_linq = e_linq.Substring(2, e_linq.Length - 2);
linq.InnerText = ServerUrlPath + e_linq;
XmlElement thumb = writer.CreateElement("media:thumbnail");
var e_thumbnail = a.ThumbnailPath;
e_thumbnail = e_thumbnail.Substring(2, e_thumbnail.Length - 2);
e_thumbnail = ServerUrlPath + e_thumbnail;
thumb.SetAttribute("url", e_thumbnail);
XmlElement content = writer.CreateElement("media:content");
content.SetAttribute("url", e_thumbnail);
id.AppendChild(name);
id.AppendChild(age);
id.AppendChild(linq);
id.AppendChild(thumb);
id.AppendChild(content);
root.AppendChild(id);
writer.AppendChild(root);
}
}
writer.Save(filename);
Result(XML File Generated)
<Channel>
<item>
<title>RedbrushAlpha.png</title>
<description>
<a href="http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png"/>
</description>
<link>
http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png
</link>
<thumbnail url="http://localhost:2023/FilesThumbs/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.Jpeg"/>
<content url="http://localhost:2023/FilesThumbs/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.Jpeg"/>
</item>
But I Need file generate similar to this with rss tag and tag like Below is the xml file code and i need to create file similar to this
<?xml version="1.0" ?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<item>
<title>mastercard.png</title>
<media:description><a href=' http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png'</media:description>
<link>http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png</link>
<media:thumbnail url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" />
<media:content url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" />
</item>
</channel>
</rss>
hi please check updated code with some changes you will now get expect output
you just need to update value of tag and your output will be ready
XmlDocument writer = new XmlDocument();
// Create XML declaration
XmlNode declaration = writer.CreateNode(XmlNodeType.XmlDeclaration, null, null);
writer.AppendChild(declaration);
//add rss node over here
XmlElement rssElement = writer.CreateElement("rss");
rssElement.SetAttribute("version", "2.0");
rssElement.SetAttribute("xmlns:media", "http://search.yahoo.com/mrss/");
rssElement.SetAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
//Channel
XmlElement channelElement = writer.CreateElement("Channel");
XmlElement itemElement = writer.CreateElement("item");
//title
XmlElement titleElement = writer.CreateElement("title");
titleElement.InnerText = "mastercard.png";
itemElement.AppendChild(titleElement);
//media:description
XmlElement media_DescriptionElement = writer.CreateElement("media", "description", "media:description");
media_DescriptionElement.InnerText = "<a href=' http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png";
itemElement.AppendChild(media_DescriptionElement);
//link
XmlElement linkElement = writer.CreateElement("link");
linkElement.InnerText = "http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png";
itemElement.AppendChild(linkElement);
//media:thumbnail
XmlElement media_ThumbnailElement = writer.CreateElement("media", "thumbnail", "media:thumbnail");
media_ThumbnailElement.SetAttribute("url", "http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg");
itemElement.AppendChild(media_ThumbnailElement);
//media:content
XmlElement media_ContentElement = writer.CreateElement("media", "content", "media:content");
media_ContentElement.SetAttribute("url", "http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg");
itemElement.AppendChild(media_ContentElement);
channelElement.AppendChild(itemElement);
rssElement.AppendChild(channelElement);
writer.AppendChild(rssElement);
Output
<?xml version="1.0"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
<Channel>
<item>
<title>mastercard.png</title>
<media:description xmlns:media="media:description"><a href=' http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png</media:description>
<link>http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png</link>
<media:thumbnail url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" xmlns:media="media:thumbnail" />
<media:content url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" xmlns:media="media:content" />
</item>
</Channel>
</rss>
Well, first and foremost, although it doesn't really change anything, it's important to realize that both XDocument and XElement accepts params, i.e. a list of items to add. This will make your code much more readable. For example:
var xdoc = new XDocument(
new XElement("root",
new XElement("foo"),
new XElement("bar")
);
);
That way the hierarchy is very clear, and you're not throwing variables all over the place.
Second, you literally made your root <channel>, rather than <rss>, so simply make <rss> your root element in your XDocument and then add <channel> to that, rather than directly to the XDocument as you're doing now.
Third, you can add attributes to elements using XAttribute. However, for namespaces, you want to persist these in a variable, so you can utilize them later as you're building your XML document. For example:
var media = XNamespace.Get("http://search.yahoo.com/mrss/");
var atom = XNamespace.Get("http://www.w3.org/2005/Atom");
var xdoc = new XDocument(
new XElement("rss",
new XAttribute("version", "2.0"),
new XAttribute(XNamespace.Xmlns + "media", media),
new XAttribute(XNamespace.Xmlns + "atom", atom),
new XElement("channel",
...
)
)
);
Then, when you need to use one of these namespaces, you concatentate it to the element name. For example, with media:thumbnail:
new XElement(media + "thumbnail",
new XAttribute("url", e_thumbnail)
)
Finally, if you use ToString on the XDocument, it will strip the XML declaration, for some boneheaded reason. Instead, you need to use a TextWriter to get the output:
var sb = new StringBuilder();
using (TextWriter writer = new Utf8StringWriter(sb))
{
xdoc.Save(writer);
}
var xmlString = sb.ToString();
FWIW, this is why you should name your XDocument variable something like xdoc rather than writer. For one, XDocument doesn't actually "write" anything, but also, writer is generally reserved for actual "writers" like TextWriter. That's all semantic, of course, but especially when dealing with XML, which usually involves voluminous amounts of code, you want it to be as readable and understandable as possible.
One last little tip. When building XML in the way I've shown here, you can pass actual enumerables just as well as single items. For example, let's say you created your list of channel items separately, such that you have a List<XElement> called items. You can actually just do:
new XElement("channel",
new XElement("title", "My Awesome Channel"),
new XElement("link", "https://foo.com/rss"),
new XElement("description", "Best channel ever!"),
items
)
EDIT
I completely forgot about Utf8StringWriter. That's a derived class of StringWriter, because StringWriter stupidly returns UTF16, and there's no way to modify that without deriving from it. The implementation is simple, but you'll need to add this to your project:
internal sealed class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder sb)
: base(sb)
{
}
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}
If you want to put this in a class library or something and share it between projects, just change internal to public.
I have a code to extract an XML document type using this web service method
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XAttribute attribute = new XAttribute(xsi + "type", "xsd:string");
XElement node_user_id = new XElement("user_id", attribute, user.code);
XDocument doc = new XDocument(new XElement("ranzcp_user", new XAttribute(XNamespace.Xmlns + "ns1", "urn:logon"), node_user_id));
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(elem.ToString());
With the above code i was able to extract an xml document exactly like this :
<ranzcp_user xmlns:ns1="urn:logon">
<user_id xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" p3:type="xsd:string">12345678</user_id>
</ranzcp_user>
But what i really need to have is this :
<ranzcp_user xmlns:ns1="urn:logon">
<user_id xsi:type="xsd:string">12345678</user_id>
</ranzcp_user>
Is there any way i could get the format i need for the xml, and second do the xsi:type="xsd:string" attribute was neccessary when the xml data was parsed?
TIA!
You could explicitly define the namespace prefix so you use the canonical xsi rather than p3:
var doc = new XDocument(
new XElement("ranzcp_user",
new XAttribute(XNamespace.Xmlns + "ns1", "urn:logon"),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XElement("user_id", 12345678,
new XAttribute(xsi + "type", "xsd:string")
)
)
);
See this fiddle. This gives you:
<ranzcp_user xmlns:ns1="urn:logon" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<user_id xsi:type="xsd:string">12345678</user_id>
</ranzcp_user>
But, as has been said, removing the namespace prefix entirely would result in invalid XML - no conforming processor will let you create it or read it without errors.
Could it be that the 'required' XML has this prefix declared in one of the parent elements? If not, I'd suggest it's a mistake and you should investigate this before spending time trying to remove the attribute. I suspect you'd end up undoing all that effort when the consumer works out the XML is invalid.
I am trying to create xml file using XDcoument, but I am getting following error
Name cannot begin with the '<' character, hexadecimal value 0x3C
here is my code
XDocument d = new XDocument(
new XElement("<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>",
new XElement("<S:Header xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>",
new XElement("<ns13:ACASecurityHeader xmlns='urn:us:gov:treasury:irs:ext:aca:air:7.0' xmlns:ns10='urn:us:gov:treasury:irs:msg:acauibusinessheader' xmlns:ns11='http://www.w3.org/2000/09/xmldsig#' xmlns:ns12='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' xmlns:ns13='urn:us:gov:treasury:irs:msg:acasecurityheader' xmlns:ns2='urn:us:gov:treasury:irs:common' xmlns:ns3='urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage' xmlns:ns4='urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage' xmlns:ns5='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:ns6='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage' xmlns:ns7='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage' xmlns:ns8='urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter' xmlns:ns9='urn:us:gov:treasury:irs:msg:acabusinessheader'>"),
new XElement("Author", "Moreno, Jordao")
),
new XElement("Book",
new XElement("Title", "Midieval Tools and Implements"),
new XElement("Author", "Gazit, Inbar")
)
),
new XComment("This is another comment."));
Can someone please help me on this?
here is sample XML file which I want to generate using XDocument
There is a much simpler way to do this rather than crafting the XML document by hand via XDocument, though I have an explanation and example below if you want to do it that way.
First, the simple way - create the XML as a string, and pass that string to XDocument.Parse, like this:
string xmlString = #"<S:Envelope xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/""><S:Header xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""><ns13:ACASecurityHeader xmlns:ns10=""urn:us:gov:treasury:irs:msg:acauibusinessheader"" xmlns:ns11=""http://www.w3.org/2000/09/xmldsig#"" xmlns:ns12=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:ns13=""urn:us:gov:treasury:irs:msg:acasecurityheader"" xmlns:ns2=""urn:us:gov:treasury:irs:common"" xmlns:ns3=""urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage"" xmlns:ns4=""urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"" xmlns:ns5=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"" xmlns:ns6=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage"" xmlns:ns7=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"" xmlns:ns8=""urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter"" xmlns:ns9=""urn:us:gov:treasury:irs:msg:acabusinessheader""><Author>Moreno, Jordao</Author><Book><Title>Midieval Tools and Implement</Title><Author>Gazit, Inbar</Author></Book></ns13:ACASecurityHeader><!--This is another comment--></S:Header></S:Envelope>";
XDocument xDoc2 = XDocument.Parse(xmlString);
xDoc2 will contain the XML you wish to send.
If you wish to do it the long way, then there are a couple of issues with your posted code.
First, you're not correctly handling the namespaces (the xmlns: attributes). Secondly, you're including the < and > in the call to XElement, and you don't need to do that - the method takes care of those two symbols.
What you need to do is to set up the namespaces, then add them to the appropriate elements as well as creating the attributes for them.
The sample code doesn't match the posted snippet, so I worked off your sample code to show you how to go about crafting the XML by hand.
XNamespace sNS = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
XNamespace wsseNS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace xmlnsNS = XNamespace.Get("urn:us:gov:treasury:irs:ext:aca:air:7.0");
XNamespace ns10NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acauibusinessheader");
XNamespace ns11NS = XNamespace.Get("http://www.w3.org/2000/09/xmldsig#");
XNamespace ns12NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace ns13NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acasecurityheader");
XNamespace ns2NS = XNamespace.Get("xmlns: ns2 = 'urn:us:gov:treasury:irs:common");
XNamespace ns3NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage");
XNamespace ns4NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage");
XNamespace ns5NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
XNamespace ns6NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage");
XNamespace ns7NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage");
XNamespace ns8NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter");
XNamespace ns9NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acabusinessheader");
XDocument xDoc = new XDocument(new XElement(sNS + "Envelope", new XAttribute(XNamespace.Xmlns + "S", sNS),
new XElement(sNS + "Header", new XAttribute(XNamespace.Xmlns + "wsse", wsseNS),
new XElement(ns13NS + "ACASecurityHeader", new XAttribute(XNamespace.Xmlns + "ns10", ns10NS),
new XAttribute(XNamespace.Xmlns + "ns11", ns11NS),
new XAttribute(XNamespace.Xmlns + "ns12", ns12NS),
new XAttribute(XNamespace.Xmlns + "ns13", ns13NS),
new XAttribute(XNamespace.Xmlns + "ns2", ns2NS),
new XAttribute(XNamespace.Xmlns + "ns3", ns3NS),
new XAttribute(XNamespace.Xmlns + "ns4", ns4NS),
new XAttribute(XNamespace.Xmlns + "ns5", ns5NS),
new XAttribute(XNamespace.Xmlns + "ns6", ns6NS),
new XAttribute(XNamespace.Xmlns + "ns7", ns7NS),
new XAttribute(XNamespace.Xmlns + "ns8", ns8NS),
new XAttribute(XNamespace.Xmlns + "ns9", ns9NS
new XAttribute("xmlns", xmlnsNS),
new XElement("Author", "Moreno, Jordao"),
new XElement("Book",
new XElement("Title", "Midieval Tools and Implement"),
new XElement("Author", "Gazit, Inbar"))
),
new XComment("This is another comment")
))
);
The first thing the above code does is sets up all the namespaces via XNamespace.
Next, the XML Document is constructed. The individual elements are created via XElement, with the various namespaces prefixed (i.e., new XElement(sNS + "Envelope",, and the other namespaces added via XAttribute.
Nesting can get tricky, so you have to be very careful doing it this way. The above code will produce the following XML:
<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<ns13:ACASecurityHeader xmlns="urn:us:gov:treasury:irs:ext:aca:air:7.0"
xmlns:ns9="urn:us:gov:treasury:irs:msg:acabusinessheader"
xmlns:ns8="urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter"
xmlns:ns7="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"
xmlns:ns6="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage"
xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:ns4="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
xmlns:ns3="urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage" xmlns:ns2="urn:us:gov:treasury:irs:common"
xmlns:ns13="urn:us:gov:treasury:irs:msg:acasecurityheader"
xmlns:ns12="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:ns11="http://www.w3.org/2000/09/xmldsig#"
xmlns:ns10="urn:us:gov:treasury:irs:msg:acauibusinessheader">
<Author>Moreno, Jordao</Author>
<Book>
<Title>Midieval Tools and Implement</Title>
<Author>Gazit, Inbar</Author>
</Book>
</ns13:ACASecurityHeader>
<!--This is another comment-->
</S:Header>
</S:Envelope>
What you're doing is a really hard way to do this. There is a much easier way.
You have the Xsd specifications from them, you can use the xsd command in the Visual Studio command line to generate C# objects that match the requirements automatically during serialization.
For the IRS ACA schemas, get all the XSD files into the same directory. Then in a sibling directory to the one you created, place the Common folder.
Then, in the command line navigate to the directory you created and put all the xsd files and run this command:
xsd /c IRS-EXT-ACA-AIR-7.0.xsd IRS-ACABulkRequestTransmitterMessage.xsd IRS-Form1094-1095CTransmitterUpstreamMessage.xsd IRS-CAC.xsd IRS-WSTimeStampElementMessage.xsd IRS-WSTimeStampElementMessage.xsd
You'll end up with a C# file that has almost 200 objects in it including all the enums and such necessary for generating data compliant with their specifications.
I need to generate an XML file using C# class (XmlDocument or XDocument) with the following root element:
<ns1:ConsultaSeqRps xmlns:ns1="http://localhost:8080/WsNFe2/lote"
xmlns:tipos="http://localhost:8080/WsNFe2/tp"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ConsultaSeqRps.xsd">
I've tried various alternatives using setAttribute and XmlNamespaceManager but without success.
It's pretty straightforward, except maybe for the use of XAttribute to add a named namespace:
XNamespace ns1 = "http://localhost:8080/WsNFe2/lote";
XNamespace tipos = "http://localhost:8080/WsNFe2/tp";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var doc = new XElement(ns1 + "ConsultaSeqRps",
new XAttribute(XNamespace.Xmlns + "ns1", ns1),
new XAttribute(XNamespace.Xmlns + "tipos", tipos),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation",
"http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ConsultaSeqRps.xsd")
);
You can take the advantage of LINQ to XML, the following article will help you to Create namespace in c#
How to: Create a Document with Namespaces (C#) (LINQ to XML)
Hope that help.
Regards
i have been searching around but I cannot seem to find any information on this issue.
if you go to https://support.google.com/webmasters/answer/34648
it says one must add the mobile tag to your xml for it to be crawled properly.
My problem lies in that I have no idea on how to actually make this tag when using XDocument.
Does anyone know how to acutally write this tag
<mobile:mobile/>
using the XElement tag?
I have the following code that generates a document
XNamespace sitemap = XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9");
XNamespace mobile = XNamespace.Get("http://www.google.com/schemas/sitemap-mobile/1.0");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(sitemap + "urlset",
new XAttribute(XNamespace.Xmlns + "mobile", mobile))
);
and the following code that builds an element
private XElement BuildSitemapItem(XNamespace ns)
{
XElement urlNode = new XElement(ns + "url",
new XElement(ns +"loc"),
new XElement(ns + "lastmod")
);
return urlNode;
}
I have been stuck on this issue for a while so any help would be appreciated.
You just need to specify the right namespace on your XElement (in this case mobile)
XNamespace mobileNs = "http://www.google.com/schemas/sitemap-mobile/1.0";
new XElement(mobileNs + "mobile")
That will output <mobile:mobile/>