GerhardvdHeever
IS-IT--Management
Hi there, I have a issue with my XML not binding the namespace, please advise, i have taken some words out and replaced with*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Schema;
using OAI_Framework.Exceptions;
using System.IO;
public string myXMLDoc(string recVerb, string recIdentifier)
{
//Document
XmlDocument _xmlDoc = new XmlDocument();
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("xmlns", " _xmlDoc.Schemas.Add(schema);
XmlDeclaration _dec = _xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
_xmlDoc.AppendChild(_dec);
//Date and Time
string recResponsDate = FormatDate(DateTime.Now, "ResponseDate");
string recDateStamp = FormatDate(DateTime.Now, "ResponseDate");
Root Element
XmlElement _root = _xmlDoc.CreateElement("OAI-PMH","
XmlAttribute _rootAtr_xmlns = _xmlDoc.CreateAttribute("xmlns", " XmlAttribute _rootAtr_xmlnsXsi = _xmlDoc.CreateAttribute("xmlns", "xsi", " XmlAttribute _rootAtr_SchemaLoc = _xmlDoc.CreateAttribute("xsi", "schemaLocation", "
_rootAtr_xmlns.Value = " _rootAtr_xmlnsXsi.Value = " _rootAtr_SchemaLoc.Value = "
_root.Attributes.Append(_rootAtr_xmlns);
_root.Attributes.Append(_rootAtr_xmlnsXsi);
_root.Attributes.Append(_rootAtr_SchemaLoc);
_xmlDoc.AppendChild(_root);
Response Date
XmlElement _responseDate = _xmlDoc.CreateElement("responseDate", " _responseDate.InnerText = recResponsDate;
_root.AppendChild(_responseDate);
Request
XmlElement _request = _xmlDoc.CreateElement("request", "
XmlAttribute _requestAtr_verb = _xmlDoc.CreateAttribute("verb", " XmlAttribute _requestAtr_ID = _xmlDoc.CreateAttribute("identifier", " XmlAttribute _requestAtr_MetaDatePrefix = _xmlDoc.CreateAttribute("metadataPrefix", "
_requestAtr_verb.Value = recVerb;
_requestAtr_ID.Value = recIdentifier;
_requestAtr_MetaDatePrefix.Value = "oai-dc";
_request.Attributes.Append(_rootAtr_xmlns);
_request.Attributes.Append(_rootAtr_xmlnsXsi);
_request.Attributes.Append(_rootAtr_SchemaLoc);
_request.InnerText = "
_root.AppendChild(_request);
Record Type to Pull (verb)
XmlElement _verbTypeToPull = _xmlDoc.CreateElement(recVerb, " _root.AppendChild(_verbTypeToPull);
Record
XmlElement _record = _xmlDoc.CreateElement("record", " _verbTypeToPull.AppendChild(_record);
Header
XmlElement _header = _xmlDoc.CreateElement("header", " identifier
XmlElement _header_SUBELEMENT_identifier = _xmlDoc.CreateElement("identifier", " _header_SUBELEMENT_identifier.InnerText= "***" + recIdentifier;
_header.AppendChild(_header_SUBELEMENT_identifier);
datestamp
XmlElement _header_SUBELEMENT_datestamp = _xmlDoc.CreateElement("datestamp", " _header_SUBELEMENT_datestamp.InnerText = recDateStamp;
_header.AppendChild(_header_SUBELEMENT_datestamp);
_record.AppendChild(_header);
Metadata
XmlElement _metadata = _xmlDoc.CreateElement("metadata", " _record.AppendChild(_metadata);
OAI_DCC
XmlElement _dc = _xmlDoc.CreateElement("oai_dc", "dc", "
XmlAttribute _dcAtr_oaiDC = _xmlDoc.CreateAttribute("xmlns", "oai_dc", " XmlAttribute _dcAtr_dc = _xmlDoc.CreateAttribute("xmlns", "dc", " XmlAttribute _dcAtr_schemaLocation = _xmlDoc.CreateAttribute("xsi","schemaLocation", "
_dcAtr_oaiDC.Value = " _dcAtr_dc.Value = " _dcAtr_schemaLocation.Value = "
_dc.Attributes.Append(_dcAtr_oaiDC);
_dc.Attributes.Append(_dcAtr_dc);
_dc.Attributes.Append(_dcAtr_schemaLocation);
ALL ELEMENTS(RECORDS) OF OAI_DC
int _dsID = Convert.ToInt32(_identifier.Substring(_identifier.LastIndexOf(":")+1));
//Get record
SqlDataReader reader = SqlHelper.ExecuteReader(_connString, "usp_AOI_GetDataset", _dsID);
//Convert to xml
if (reader.Read())
{
0
if (!reader.IsDBNull(0)) //identifier
{
XmlNode _emtIdentifier = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "identifier", " _emtIdentifier.InnerXml = reader["identifier"].ToString();
_dc.AppendChild(_emtIdentifier);
}
1
if (!reader.IsDBNull(1)) //title
{
XmlNode _emtTitle = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "title", " _emtTitle.InnerXml = reader["title"].ToString();
_dc.AppendChild(_emtTitle);
}
2
if (!reader.IsDBNull(2)) //creator
{
XmlNode _emtCreator = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "creator", " _emtCreator.InnerXml = reader["creator"].ToString();
_dc.AppendChild(_emtCreator);
}
3
if (!reader.IsDBNull(3)) //description
{
XmlNode _emtDescription = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "description", " _emtDescription.InnerXml = reader["description"].ToString();
_dc.AppendChild(_emtDescription);
}
date
XmlNode _emtDate = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "date", " _emtDate.InnerXml = recDateStamp;
_dc.AppendChild(_emtDate);
}
StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Namespaces = true;
_xmlDoc.WriteTo(xmlWriter);
xmlWriter.Flush();
strWriter.Flush();
return strWriter.ToString();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Schema;
using OAI_Framework.Exceptions;
using System.IO;
public string myXMLDoc(string recVerb, string recIdentifier)
{
//Document
XmlDocument _xmlDoc = new XmlDocument();
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("xmlns", " _xmlDoc.Schemas.Add(schema);
XmlDeclaration _dec = _xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
_xmlDoc.AppendChild(_dec);
//Date and Time
string recResponsDate = FormatDate(DateTime.Now, "ResponseDate");
string recDateStamp = FormatDate(DateTime.Now, "ResponseDate");
Root Element
XmlElement _root = _xmlDoc.CreateElement("OAI-PMH","
XmlAttribute _rootAtr_xmlns = _xmlDoc.CreateAttribute("xmlns", " XmlAttribute _rootAtr_xmlnsXsi = _xmlDoc.CreateAttribute("xmlns", "xsi", " XmlAttribute _rootAtr_SchemaLoc = _xmlDoc.CreateAttribute("xsi", "schemaLocation", "
_rootAtr_xmlns.Value = " _rootAtr_xmlnsXsi.Value = " _rootAtr_SchemaLoc.Value = "
_root.Attributes.Append(_rootAtr_xmlns);
_root.Attributes.Append(_rootAtr_xmlnsXsi);
_root.Attributes.Append(_rootAtr_SchemaLoc);
_xmlDoc.AppendChild(_root);
Response Date
XmlElement _responseDate = _xmlDoc.CreateElement("responseDate", " _responseDate.InnerText = recResponsDate;
_root.AppendChild(_responseDate);
Request
XmlElement _request = _xmlDoc.CreateElement("request", "
XmlAttribute _requestAtr_verb = _xmlDoc.CreateAttribute("verb", " XmlAttribute _requestAtr_ID = _xmlDoc.CreateAttribute("identifier", " XmlAttribute _requestAtr_MetaDatePrefix = _xmlDoc.CreateAttribute("metadataPrefix", "
_requestAtr_verb.Value = recVerb;
_requestAtr_ID.Value = recIdentifier;
_requestAtr_MetaDatePrefix.Value = "oai-dc";
_request.Attributes.Append(_rootAtr_xmlns);
_request.Attributes.Append(_rootAtr_xmlnsXsi);
_request.Attributes.Append(_rootAtr_SchemaLoc);
_request.InnerText = "
_root.AppendChild(_request);
Record Type to Pull (verb)
XmlElement _verbTypeToPull = _xmlDoc.CreateElement(recVerb, " _root.AppendChild(_verbTypeToPull);
Record
XmlElement _record = _xmlDoc.CreateElement("record", " _verbTypeToPull.AppendChild(_record);
Header
XmlElement _header = _xmlDoc.CreateElement("header", " identifier
XmlElement _header_SUBELEMENT_identifier = _xmlDoc.CreateElement("identifier", " _header_SUBELEMENT_identifier.InnerText= "***" + recIdentifier;
_header.AppendChild(_header_SUBELEMENT_identifier);
datestamp
XmlElement _header_SUBELEMENT_datestamp = _xmlDoc.CreateElement("datestamp", " _header_SUBELEMENT_datestamp.InnerText = recDateStamp;
_header.AppendChild(_header_SUBELEMENT_datestamp);
_record.AppendChild(_header);
Metadata
XmlElement _metadata = _xmlDoc.CreateElement("metadata", " _record.AppendChild(_metadata);
OAI_DCC
XmlElement _dc = _xmlDoc.CreateElement("oai_dc", "dc", "
XmlAttribute _dcAtr_oaiDC = _xmlDoc.CreateAttribute("xmlns", "oai_dc", " XmlAttribute _dcAtr_dc = _xmlDoc.CreateAttribute("xmlns", "dc", " XmlAttribute _dcAtr_schemaLocation = _xmlDoc.CreateAttribute("xsi","schemaLocation", "
_dcAtr_oaiDC.Value = " _dcAtr_dc.Value = " _dcAtr_schemaLocation.Value = "
_dc.Attributes.Append(_dcAtr_oaiDC);
_dc.Attributes.Append(_dcAtr_dc);
_dc.Attributes.Append(_dcAtr_schemaLocation);
ALL ELEMENTS(RECORDS) OF OAI_DC
int _dsID = Convert.ToInt32(_identifier.Substring(_identifier.LastIndexOf(":")+1));
//Get record
SqlDataReader reader = SqlHelper.ExecuteReader(_connString, "usp_AOI_GetDataset", _dsID);
//Convert to xml
if (reader.Read())
{
0
if (!reader.IsDBNull(0)) //identifier
{
XmlNode _emtIdentifier = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "identifier", " _emtIdentifier.InnerXml = reader["identifier"].ToString();
_dc.AppendChild(_emtIdentifier);
}
1
if (!reader.IsDBNull(1)) //title
{
XmlNode _emtTitle = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "title", " _emtTitle.InnerXml = reader["title"].ToString();
_dc.AppendChild(_emtTitle);
}
2
if (!reader.IsDBNull(2)) //creator
{
XmlNode _emtCreator = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "creator", " _emtCreator.InnerXml = reader["creator"].ToString();
_dc.AppendChild(_emtCreator);
}
3
if (!reader.IsDBNull(3)) //description
{
XmlNode _emtDescription = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "description", " _emtDescription.InnerXml = reader["description"].ToString();
_dc.AppendChild(_emtDescription);
}
date
XmlNode _emtDate = _xmlDoc.CreateNode(XmlNodeType.Element, "dc", "date", " _emtDate.InnerXml = recDateStamp;
_dc.AppendChild(_emtDate);
}
StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Namespaces = true;
_xmlDoc.WriteTo(xmlWriter);
xmlWriter.Flush();
strWriter.Flush();
return strWriter.ToString();
}