<?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>
我的代码:
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);
我需要创建以下XML,并且我正在尝试使用XDocument执行此操作。但失败无法创建xml文件?
如何为此xml文件创建xsd架构?
答案 0 :(得分:0)
请尝试以下操作:
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);
}
}
}