I need to add multiple namespaces to the xdocument with same address (C#)
<root xmlns:f="urn://xml.voodoo.net/vd/formating-1.0" xmlns="urn://xml.voodoo.net/vd/formating-1.0">
<a:something>stuff and more stuff</a:something>
</root>
if i add using the below code.. it shows only xmlns:f
XNamespace defaultNS = "urn://xml.voodoo.net/vd/formating-1.0";
XNamespace f = "urn://xml.voodoo.net/vd/formating-1.0";
XElement rootElement = new XElement(defaultNS + "root",
new XAttribute(XNamespace.Xmlns + "f", f.NamespaceName),
how to show 2 namespaces?? is it even possible?
var doc = new XDocument(
new XElement(defaultNS + "root",
new XAttribute(XNamespace.Xmlns + "f", defaultNS),
new XAttribute("xmlns", defaultNS),
new XElement(defaultNS + "something",
new XAttribute(XNamespace.Xmlns + "f", defaultNS), "stuff and more stuff")
)
);
Desired output:
<root xmlns:f="urn://xml.voodoo.net/vd/formating-1.0" xmlns="urn://xml.voodoo.net/vd/formating-1.0">
<f:something xmlns:f="urn://xml.voodoo.net/vd/formating-1.0">stuff and more stuff</f:something>
</root>
Your XML example is incorrect, the 'f' namespace is not used, whereas there is an 'a' namespace present.
Your C# code does not match your XML, it creates an element with an attribute.
Anyhow, a namespace definitions within an XML document only makes sense if you actually use it. If you create an XML document via C# code, it will generate XML which is semantically correct, yet might not match the syntax of your example.
Related
I try programming the following xml document (simplified) as code:
<P xmlns="http://schemas.microsoft.com/x/2010/manifest"
xmlns:ab="http://schemas.microsoft.com/a/2010/manifest"
xmlns:ac="http://schemas.microsoft.com/a/2013/manifest">
<ab:Ex xmlns:ab="http://schemas.microsoft.com/a/2013/manifest">
</ab:Ex>
</P>
Now an element redefines the namespace:
xmlns:ab="http://schemas.microsoft.com/a/2013/manifest".
how do I get the element into my program? It is obviously valid because I can load the xml file like this:
Add(new XAttribute (XNamespace.Xmlns + "ab")
I get the error message:
System.Xml.XmlException: 'The prefix 'ab' cannot be redefined from 'http://schemas.microsoft.com/a/2010/manifest' to 'http://schemas.microsoft.com/a/2013/manifest' within the same start element tag.'
Actually logical (redefinition) but when I load the document, the element is accepted.
using System.Xml.Linq;
namespace xmltest
{
class Program
{
static void Main(string[] args)
{
//Load
XDocument doc1 = XDocument.Load(#"c:\simple.xml", LoadOptions.None);
System.Console.WriteLine(doc1.ToString());
//Create new
XNamespace ab = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace nsx = "http://schemas.microsoft.com/x/2010/manifest";
XDocument doc2 = new XDocument(new XDeclaration("1.0", "utf-8", ""),
new XElement(nsx + "P"));
doc2.Element(nsx + "P").Add(new XAttribute("xmlns",
"http://schemas.microsoft.com/x/2010/manifest"));
doc2.Element(nsx + "P").Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2010/manifest"));
doc2.Element(nsx + "P").Add(new XAttribute(XNamespace.Xmlns + "ac",
"http://schemas.microsoft.com/a/2013/manifest"));
XElement xml = new XElement(ab + "Ex");
//--> Below does not work work
xml.Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2013/manifest"));
doc2.Element(nsx + "P").Add(xml);
System.Console.WriteLine(doc2.ToString());
}
}
}
Anybody have any idea how to solve this problem?
In theory, because
<ab:Ex xmlns:ab="http://schemas.microsoft.com/a/2013/manifest">
actually means that Ex is to be defined in the new namespace (2013), you can get what you want by defining Ex in the new namespace, i.e. something like:
XNamespace abOld = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace abNew = "http://schemas.microsoft.com/a/2013/manifest";
XNamespace nsx = ...
XElement xml = new XElement(abNew + "Ex");
xml.Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2013/manifest"));
However, repurposing the alias of an existing namespace just so that you can use the same alias is going to be really confusing to future maintenance - as the alias leaves scope, it will revert to it's old definition. I would recommend that you at least create different, unique aliases for the 2010 and 2013 namespaces.
Also, unless the intended consumer of the Xml was buggy, and required specific aliases or namespace scopes, I would generally avoid micro-managing the namespace aliasing and namespace scoping - let Linq to Xml manage that for you.
For instance, how about just:
XNamespace abOld = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace abNew = "http://schemas.microsoft.com/a/2013/manifest";
// Build up the document by providing element, attribute + applicable namespaces:
var root = new XElement(abOld + "P",
new XElement(abNew + "Ex"));
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 am creating xml in C#, and want to add Namespace and Declaration . My xml as below:
XNamespace ns = "http://ab.com//abc";
XDocument myXML = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XElement(ns + "Root",
new XElement("abc","1")))
This will add xmlns="" at both Root level and child element abc level as well.
<Root xmlns="http://ab.com/ab">
<abc xmlns=""></abc>
</Root>
But I want it in only Root level not in child level like below:
<Root xmlns="http://ab.com/ab">
<abc></abc>
</Root>
and how to add Declaration at Top, my code not showing Declaration after run.
Please help me to get the complete xml as
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root xmlns="http://ab.com/ab">
<abc></abc>
</Root>
You need to use the same namespace in your child elements:
XDocument myXML = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XElement(ns + "Root",
new XElement(ns + "abc", "1")))
If you just use "abc" this will be converted to an XName with no Namespace. This then results in an xmlns="" attribute being added so the fully qualified element name for abc would be resolved as such.
By setting the name to ns + "abc" no xmlns attribute will be added when converted to string as the default namespace of http://ab.com/ab is inherited from Root.
If you wanted to simply 'inherit' the namespace then you wouldn't be able to do this in such a fluent way. You'd have create the XName using the namespace of the parent element, for example:
var root = new XElement(ns + "Root");
root.Add(new XElement(root.Name.Namespace + "abc", "1"));
Regarding the declaration, XDocument doesn't include this when calling ToString. It will if you use Save to write to a Stream, TextWriter, or if you supply an XmlWriter that doesn't have OmitXmlDeclaration = true in its XmlWriterSettings.
If you wanted to just get the string, this question has an answer with a nifty extension method using a StringWriter.
Use the namespace on all elements you create:
XDocument myXML = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XElement(ns + "Root",
new XElement(ns + "abc","1")))
I am creating XML using Linq to XML and C#. It all works great except when I need to manually add in a row in the XML. This row is only added if I have a value to pass through to it, otherwise I just ignore the entire tag.
I use XElement.Load to load in the string of text that I store in a string but when I attach it to the XML it always puts in xmlns="" at the end of my tag.
Is there a way I can tell XElement.Load to use the existing namespace or to ignore it when putting in the string to the XML?
Ideally I just want my string to be included to the XML being created without extra tags being added.
Below is a sample of what I currently do:
string XMLDetails = null;
if (ValuePassedThrough != null)
XMLDetails = "<MyNewTag Code=\"14\" Value=\"" + ValuePassedThrough +"\"></MyNewTag>";
When I build up the XML I load the above string into my XML. It is here where xmlns="" is being added to the XMLDetails value but ideally I want this ignored as it is causing issues with the recipient when they try to read this tag.
XNamespace ns = "http://namespace-address";
XNamespace xsi = "http://XMLSchema-instance-address";
XDocument RequestDoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(ns + "HeaderTag",
new XAttribute("xmlns", ns),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", "http://www.addressofschema.xsd"),
new XAttribute("Version", "1"),
new XElement(ns + "OpeningTAG",
...My XML Code ...
XElement.Load(new StringReader(XMLDetails))
...End of XML Code ...
As mentioned above. My code works, it successfully outputs XML for me. Its just the MyNewTag tag I load in using XElement.Load gets xmlns="" added to the end of it which is causing me an issue.
Any ideas how I can work around this? Thanks for your help.
Regards,
Rich
How about:
XElement withoutNamespace = XElement.Load(new StringReader(XMLDetails));
XElement withNamespace = new XElement(ns + withoutNamespace.Name.LocalName,
withoutNamespace.Nodes());
As a better alternative - why don't you either include the namespace when you're building up the XML, or even better, create an XElement instead of manually generating an XML string which you then read. Manually creating XML is very rarely a good idea. Aside from anything else, you're assuming that ValuePassedThrough has already been escaped, or doesn't need escaping etc. That may be valid - but it's at least a cause for concern.
Like this
XElement XMLDetails = new XElement(ns + "OpeningTAG", new XElement(ns + "MyNewTag", new XAttribute("Code", 14), new XAttribute("Value", 123)));
XDocument RequestDoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(ns + "HeaderTag",
new XAttribute("xmlns", ns),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", "http://www.addressofschema.xsd"),
new XAttribute("Version", "1"),
XMLDetails));