|
Source code:
XElement Listings = new XElement("Listings");
XNamespace root = "http://website.RFGCommonSpecListingSchema";
XNamespace xs = "http://www.w3.org/2001/XMLSchema";
XNamespace b = "http://schemas.microsoft.com/BizTalk/2003";
Listings.Add(new XDeclaration("1.0", "utf-8", null),
new XElement(xs + "schema",
new XAttribute("xmlns", root.NamespaceName),
new XAttribute(XNamespace.Xmlns + "b", b.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xs", xs.NamespaceName),
new XAttribute("targetNamespace", "http://rfg.realogy.com/Btt/Listingfeed/Import/2011/07.RFGCommonSpecListingSchema"),
new XAttribute("elementFormDefault", "qualified"),
new XAttribute("attributeFormDefault", "unqualified")));
XElement Listing;
string command = getQuery();
using (SqlConnection sqlCon = new SqlConnection(connMain))
{
sqlCon.Open();
using (SqlCommand cmd = new SqlCommand(command, sqlCon))
{
using (SqlDataReader Row = cmd.ExecuteReader())
{
while (Row.Read())
{
featureIN.Clear();
#region XML build
Listings.Add(Listing = new XElement("Listing"));
Listings.Add(
new XElement("ListingKey", Row[3].ToString()),
new XElement("ListingIdentifier", Row[3].ToString()),
new XElement("Type", Row[4].ToString()),
new XElement("ProviderName", "CWS"));
result:
- <Listings>
="1.0"="utf-8"
<xs:schema xmlns="http://website.RFGCommonSpecListingSchema" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://rfg.realogy.com/Btt/Listingfeed/Import/2011/07.RFGCommonSpecListingSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" />
<Listing />
<ListingKey>1311618</ListingKey>
<ListingIdentifier>1311618</ListingIdentifier>
<Type>Residential Sale</Type>
<ProviderName>CWS</ProviderName>
<Listing />
<ListingKey>1715675</ListingKey>
<ListingIdentifier>1715675</ListingIdentifier>
<Type>Residential Sale</Type>
<ProviderName>CWS</ProviderName>
<Listing />
I need the header tags above the 'Listings' tag like this:
="1.0"="utf-16"
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://rfg.realogy.com/Btt/Listingfeed/Import/2011/07.RFGCommonSpecListingSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://rfg.realogy.com/Btt/Listingfeed/Import/2011/07.RFGCommonSpecListingSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Listings">
<xs:complexType>
<xs:sequence>
<xs:element name="Listing" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
Anyone have a code that would work? Where am I going wrong with this?
|
|
|
|
|
You need a root XDocument to contain the XElement Listings . That is where the declaration and schema tags need to be. And I'd guess that you don't need the schema element, but put the namespace attributes on the Listings (you aren't declaring a schema here, and you can't have two root elements in a valid XML file):
XNamespace root = "http://website.RFGCommonSpecListingSchema";
XNamespace xs = "http://www.w3.org/2001/XMLSchema";
XNamespace b = "http://schemas.microsoft.com/BizTalk/2003";
XElement Listings = new XElement(root + "Listings",
new XAttribute(XNamespace.Xmlns + "b", b.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xs", xs.NamespaceName),
new XAttribute("targetNamespace", "http://rfg.realogy.com/Btt/Listingfeed/Import/2011/07.RFGCommonSpecListingSchema"));
XDocument listingDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), Listings);
XElement Listing;
string command = getQuery();
This gives the following XML:
="1.0"="utf-16"
<Listings xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://rfg.realogy.com/Btt/Listingfeed/Import/2011/07.RFGCommonSpecListingSchema" xmlns="http://website.RFGCommonSpecListingSchema" />
The Listing element(s) will be contained in the Listings element (that I've shown as empty).
Be sure when you create the contained XElement (s) that you create them in the same namespace as the Listings element. I.e., new XElement(root + "Listing") or new XElement(root + "ProviderName") , etc.
|
|
|
|