<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 (http://www.altova.com)-->
<ftc:FATCA_OECD version="2.0" xsi:schemaLocation="urn:oecd:ties:fatca:v2 FatcaXML_v2.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ftc="urn:oecd:ties:fatca:v2" xmlns:sfa="urn:oecd:ties:stffatcatypes:v2">
<ftc:MessageSpec>
<sfa:SendingCompanyIN>S519K4.99999.SL.392</sfa:SendingCompanyIN>
<sfa:TransmittingCountry>JP</sfa:TransmittingCountry>
<sfa:ReceivingCountry>US</sfa:ReceivingCountry>
<sfa:MessageType>FATCA</sfa:MessageType>
<sfa:MessageRefId>DBA6455E-8454-47D9-914B-FEE48E4EF3AA</sfa:MessageRefId>
<sfa:ReportingPeriod>2016-12-31</sfa:ReportingPeriod>
<sfa:Timestamp>2017-01-17T09:30:47Z</sfa:Timestamp>
</ftc:MessageSpec>
</ftc:FATCA_OECD>
my code:
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
XmlElement element1 = doc.CreateElement("<ftc:FATCA_OECD version=\"2.0\" xsi:schemaLocation=\"urn:oecd:ties:fatca:v2 FatcaXML_v2.0.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ftc=\"urn:oecd:ties:fatca:v2\" xmlns:sfa=\"urn:oecd:ties:stffatcatypes:v2\">");
doc.AppendChild(element1);
XmlElement element2 = doc.CreateElement("ftc:MessageSpec");
element1.AppendChild(element2);
XmlElement element3 = doc.CreateElement("sfa:SendingCompanyIN");
XmlText text1 = doc.CreateTextNode(txt_SendingCompanyIN.Text);
element3.AppendChild(text1);
element2.AppendChild(element3);
XmlElement element4 = doc.CreateElement("sfa:TransmittingCountry");
XmlText text2 = doc.CreateTextNode(txt_TransmittingCountry.Text);
element4.AppendChild(text2);
element2.AppendChild(element4);
XmlElement element5 = doc.CreateElement("sfa:ReceivingCountry");
XmlText text3 = doc.CreateTextNode(txt_ResCountryCode.Text);
element4.AppendChild(text3);
element2.AppendChild(element4);
I need to create the following XML and I'm trying to do this using XDocument. but fail cannot create xml file?
how can i create a xsd schema for this xml file?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication48
{
class Program
{
static void Main(string[] args)
{
//<?xml version="1.0" encoding="UTF-8"?>
//<!--Sample XML file generated by XMLSpy v2013 (http://www.altova.com)-->
//<ftc:FATCA_OECD version="2.0" xsi:schemaLocation="urn:oecd:ties:fatca:v2 FatcaXML_v2.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ftc="urn:oecd:ties:fatca:v2" xmlns:sfa="urn:oecd:ties:stffatcatypes:v2">
// <ftc:MessageSpec>
// <sfa:SendingCompanyIN>S519K4.99999.SL.392</sfa:SendingCompanyIN>
// <sfa:TransmittingCountry>JP</sfa:TransmittingCountry>
// <sfa:ReceivingCountry>US</sfa:ReceivingCountry>
// <sfa:MessageType>FATCA</sfa:MessageType>
// <sfa:MessageRefId>DBA6455E-8454-47D9-914B-FEE48E4EF3AA</sfa:MessageRefId>
// <sfa:ReportingPeriod>2016-12-31</sfa:ReportingPeriod>
// <sfa:Timestamp>2017-01-17T09:30:47Z</sfa:Timestamp>
// </ftc:MessageSpec>
//</ftc:FATCA_OECD>
string header =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ftc:FATCA_OECD" +
" version=\"2.0\" xsi:schemaLocation=\"urn:oecd:ties:fatca:v2 FatcaXML_v2.0.xsd\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:ftc=\"urn:oecd:ties:fatca:v2\"" +
" xmlns:sfa=\"urn:oecd:ties:stffatcatypes:v2\">" +
"</ftc:FATCA_OECD>";
XDocument doc = XDocument.Parse(header);
XElement FATCA_OECD = (XElement)doc.FirstNode;
XNamespace ftcNS = FATCA_OECD.GetNamespaceOfPrefix("ftc");
XNamespace sfaNS = FATCA_OECD.GetNamespaceOfPrefix("sfa");
XElement MessageSpec = new XElement(ftcNS + "MessageSpec", new object[] {
new XElement(sfaNS + "SendingCompanyIN", "S519K4.99999.SL.392"),
new XElement(sfaNS + "TransmittingCountry", "JP"),
new XElement(sfaNS + "ReceivingCountry", "US"),
new XElement(sfaNS + "MessageType", "FATCA"),
new XElement(sfaNS + "MessageRefId", "DBA6455E-8454-47D9-914B-FEE48E4EF3AA"),
new XElement(sfaNS + "ReportingPeriod", "2016-12-31"),
new XElement(sfaNS + "Timestamp", "2017-01-17T09:30:47Z"),
});
FATCA_OECD.Add(MessageSpec);
}
}
}
Related
I am bad at xml understanding, please help me
this is xml that I need
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ENRC-IDOC_939_DigiDocs:idm">
<soapenv:Header />
<soapenv:Body>
<urn:mt_digidocs_fees>
This is my code
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration( "1.0", "UTF-8", null );
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
XmlElement envelope = doc.CreateElement("soapenv","Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
XmlAttribute urn = doc.CreateAttribute("xmlns","urn", "http://www.w3.org/2000/xmlns/");
urn.Value = "urn:ENRC-IDOC_939_DigiDocs:idm";
envelope.Attributes.SetNamedItem(urn);
doc.AppendChild(envelope);
XmlNode soapenvheader = doc.CreateElement("soapenv", "Header", doc.DocumentElement.NamespaceURI);
envelope.AppendChild(soapenvheader);
XmlNode body = doc.CreateElement("soapenv", "Body", doc.DocumentElement.NamespaceURI);
envelope.AppendChild(body);
XmlElement mt_digidocs_fees = doc.CreateElement("mt_digidocs_fees");
mt_digidocs_fees.Prefix = "urn";
body.AppendChild(mt_digidocs_fees);
and this is what I get
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:urn="urn:ENRC-IDOC_939_DigiDocs:idm" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header />
<soapenv:Body>
<mt_digidocs_fees>
So there last Element's name does not contain prefix and namespace addresses are in different order
Thanks in advance
I like using Xml Linq 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 ident = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:ENRC-IDOC_939_DigiDocs:idm\"></soapenv:Envelope>";
XDocument doc = XDocument.Parse(ident);
XElement envelope = doc.Root;
XNamespace nsUrn = envelope.GetNamespaceOfPrefix("urn");
XNamespace nsSoapenv = envelope.GetNamespaceOfPrefix("soapenv");
XElement header = new XElement(nsSoapenv + "Header");
envelope.Add(header);
XElement body = new XElement(nsSoapenv + "Body");
envelope.Add(body);
XElement fees = new XElement(nsUrn + "mt_digidocs_fees");
body.Add(fees);
}
}
}
I need to create an xml which it will look like xml below:
<?xml version="1.0"?>
-<env:Envelope xmlns:ns3="http://rgwspublic2/RgWsPublic2" xmlns:ns2="http://rgwspublic2/RgWsPublic2Service" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://www.w3.org/2003/05/soap-envelope">
-<env:Header>
-<ns1:Security>
-<ns1:UsernameToken>
<ns1:Username>xxxx</ns1:Username>
<ns1:Password>yyyy</ns1:Password>
</ns1:UsernameToken>
</ns1:Security>
</env:Header>
-<env:Body>
-<ns2:rgWsPublic2AfmMethod>
-<ns2:INPUT_REC>
<ns3:afm_called_by/>
<ns3:afm_called_for>xxxxxxxxx</ns3:afm_called_for>
</ns2:INPUT_REC>
</ns2:rgWsPublic2AfmMethod>
</env:Body>
</env:Envelope>
I am new in xmls. How can i create an xml like this?
Also if is it easier to create an xml from a list collection?
Then i try to make a request like example below
HttpClient httpClient = new HttpClient();
var response = await httpClient.PostAsync("https://www1.gsis.gr:443", new StringContent(booksFromFile.ToString(), Encoding.UTF8, "application/xml"));
var HttpResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine(HttpResponse);
But i am getting error
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="REFRESH" content="0;url=http://www.gsis.gr"></HEAD>
<BODY>
</BODY>
</HTML>
Try xml linq :
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\"?>" +
"<env:Envelope" +
" xmlns:ns3=\"http://rgwspublic2/RgWsPublic2\"" +
" xmlns:ns2=\"http://rgwspublic2/RgWsPublic2Service\"" +
" xmlns:ns1=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"" +
" xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">" +
"</env:Envelope>";
XDocument doc = XDocument.Parse(xml);
XElement root = doc.Root;
XNamespace nsEnv = root.GetNamespaceOfPrefix("env");
XNamespace ns1 = root.GetNamespaceOfPrefix("ns1");
XNamespace ns2 = root.GetNamespaceOfPrefix("ns2");
XNamespace ns3 = root.GetNamespaceOfPrefix("ns3");
XElement header = new XElement(nsEnv + "Header",
new XElement(ns1 + "Security",
new XElement(ns1 + "UsernameToken")));
XElement usernameToken = header.Descendants(ns1 + "UsernameToken").FirstOrDefault();
string username = "xxxxx";
string password = "yyyy";
usernameToken.Add(new XElement(ns1 + "Username", username));
usernameToken.Add(new XElement(ns1 + "Password", password));
root.Add(header);
XElement body = new XElement(nsEnv + "Body",
new XElement(ns2 + "rgWsPublic2AfmMethod",
new XElement(ns2 + "INPUT_REC")));
XElement inputRec = body.Descendants(ns2 + "INPUT_REC").FirstOrDefault();
root.Add(body);
string afmCalledFor = "xxxxxxxxx";
inputRec.Add(new XElement(ns3 + "afm_called_by"));
inputRec.Add(new XElement(ns3 + "afm_called_for", afmCalledFor));
}
}
}
i create an XMLDocument:
`
XmlDocument doc = new XmlDocument();
XmlDeclaration declaire = doc.CreateXmlDeclaration("1.0", "utf-8", null);
// -----------------------create root-----------------------------
XmlElement rootnode = doc.CreateElement( "BMECAT");
doc.InsertBefore(declaire, doc.DocumentElement);
doc.AppendChild(rootnode);
//Console.WriteLine(sb.ToString());
//get attribute for BmeCat
rootnode.SetAttribute("version", "2005");
XmlAttribute atr = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
atr.Value = "http://www.adlnet.org/xsd/adlcp_v1p3";
rootnode.SetAttributeNode(atr);
rootnode.Attributes.Append(atr);`
then i convert it to XDocument using the function below but i get a NameSpace changed like this
XDocument ToXDocument(XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
`
below the xml and the XDocument
<?xml version="1.0" encoding="utf-8"?>
<BMECAT version="2005" p1:schemaLocation="http://www.adlnet.org/xsd/adlcp_v1p3" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance">
<?xml version="1.0" encoding="utf-8"?>
<BMECAT version="2005" xsi:schemaLocation="http://www.adlnet.org/xsd/adlcp_v1p3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
You need to explicitly add the xsi namespace declaration manually at the beginning of your element.
Nodes are processed in document order and the namespace declaration appears later after it is first used. It doesn't know to use xsi for the prefix until it's too late.
var doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
var root = (XmlElement)doc.AppendChild(doc.CreateElement("BMECAT"));
var xsi = "http://www.w3.org/2001/XMLSchema-instance";
root.SetAttribute("xmlns:xsi", xsi); //set the namespace now
root.SetAttribute("schemaLocation", xsi, "http://www.adlnet.org/xsd/adlcp_v1p3");
root.SetAttribute("version", "2005");
I have an xml file with attribute
xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
When I remove this attribute, then I can read each tag. If I don't remove attribute I receive error message "Object refrence not set to instance of object"
My C# code:
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("~/HotelSearchCriteria/PrepareBooking.xml"));
string CommmitLevel = xml.DocumentElement
.SelectSingleNode("/BookingCreate/CommitLevel")
.InnerText;
My XML:
<BookingCreate xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
<Authority>
<Org>danco</Org>
<User>xmltest</User>
<Password>xmltest</Password>
<Language>en</Language>
<Currency>EUR</Currency>
<TestDebug>false</TestDebug>
<Version>1.26</Version>
</Authority>
<QuoteId>17081233-3</QuoteId>
<HotelStayDetails>
<Room>
<Guests>
<Adult title="Mr" first="djkvb" last="jkj" />
<Adult title="Mr" first="jfs" last="kjdjs" />
</Guests>
</Room>
</HotelStayDetails>
<HotelSearchCriteria>
<AvailabilityStatus>allocation</AvailabilityStatus>
<DetailLevel>basic</DetailLevel>
</HotelSearchCriteria>
<CommitLevel>prepare</CommitLevel>
</BookingCreate>
What to do to read xml with xmlns attribute? It is necessary for me to have xmlns attribute.
You need to specify the namespace, by using a namespace manager. This should work
XmlDocument bookingXml = new XmlDocument();
bookingXml.Load("PrepareBooking.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(bookingXml.NameTable);
ns.AddNamespace("booking", "http://www.reservwire.com/namespace/WebServices/Xml");
var commitLevel = bookingXml.SelectSingleNode("//booking:BookingCreate//booking:CommitLevel", ns).InnerText;
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement bookingCreate = doc.Descendants().Where(x => x.Name.LocalName == "BookingCreate").FirstOrDefault();
XNamespace ns = bookingCreate.GetDefaultNamespace();
var results = doc.Descendants(ns + "BookingCreate").Select(x => new {
org = (string)x.Descendants(ns + "Org").FirstOrDefault(),
user = (string)x.Descendants(ns + "User").FirstOrDefault(),
password = (string)x.Descendants(ns + "Password").FirstOrDefault(),
language = (string)x.Descendants(ns + "Language").FirstOrDefault(),
currency = (string)x.Descendants(ns + "Currency").FirstOrDefault(),
testDebug = (Boolean)x.Descendants(ns + "TestDebug").FirstOrDefault(),
version = (string)x.Descendants(ns + "Version").FirstOrDefault(),
quoteId = (string)x.Element(ns + "QuoteId"),
guests = x.Descendants(ns + "Adult").Select(y => new {
title = (string)y.Attribute("title"),
first = (string)y.Attribute("first"),
last = (string)y.Attribute("last")
}).ToList(),
availability = (string)x.Descendants(ns + "AvailabilityStatus").FirstOrDefault(),
detailLevel = (string)x.Descendants(ns + "DetailLevel").FirstOrDefault()
}).FirstOrDefault();
}
}
}
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));
}
}
}