I am developing an application in c# that takes input and generate xml file and displays as a document at the ouput. I wrote a class and properties to pass the parameters from the class to generate the xml file. The sample code I wrote as follows:
serialXml.cs file
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace sampleXml
{
[XmlRoot("ClinicalDocument")]
public class Patient
{
public Patient()
{
//default constructor
}
private String firstName;
private String lastName;
private String gender;
private String dateOfBirth;
private String providerOrgName;
private String assignedAuthorFirstName;
private String assignedAuthorLastName;
private String representOrgName;
private String custodianOrgName;
public String FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public String LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public String Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
public String DateOfBirth
{
get
{
return dateOfBirth;
}
set
{
dateOfBirth = value;
}
}
public String ProviderOrgName
{
get
{
return providerOrgName;
}
set
{
providerOrgName = value;
}
}
public String AssignedAuthorFirstName
{
get
{
return assignedAuthorFirstName;
}
set
{
assignedAuthorFirstName = value;
}
}
public String AssignedAuthorLastName
{
get
{
return assignedAuthorLastName;
}
set
{
assignedAuthorLastName = value;
}
}
public String RepresentOrgName
{
get
{
return representOrgName;
}
set
{
representOrgName = value;
}
}
public String CustodianOrgName
{
get
{
return custodianOrgName;
}
set
{
custodianOrgName = value;
}
}
}
}
Codebind file code:
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
namespace sampleXml
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WriteXML();
string strFile = @"c:\list.xml";
outputBox.Text= GetXmlString(strFile);
}
void WriteXML()
{
//let's try to save the XML document in a file: C:\pavel.xml
try
{
Patient patientObj = new Patient();
patientObj.FirstName = "Naresh";
patientObj.LastName = "Valluri";
patientObj.DateOfBirth = "01/01/2000";
patientObj.ProviderOrgName = "Comdyn";
patientObj.AssignedAuthorFirstName = "northFirst";
patientObj.AssignedAuthorLastName = "northLast";
patientObj.RepresentOrgName = "charlotteOrg";
patientObj.CustodianOrgName = "devlopCust";
// Serialization
XmlSerializer s = new XmlSerializer(typeof(Patient));
TextWriter w = new StreamWriter(@"c:\list.xml");
s.Serialize(w, patientObj);
w.Close();
string strFilename = @"c:\list.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(strFilename))
{
xmlDoc.Load(strFilename);
XmlElement elmRoot = xmlDoc.DocumentElement;
XmlElement elmNew = xmlDoc.CreateElement("typeId");
XmlAttribute NodeAtt = xmlDoc.CreateAttribute("root");
NodeAtt.Value = "2.16.840.1.113883.1.3";
elmNew.SetAttributeNode(NodeAtt);
XmlAttribute NodeAtt1 = xmlDoc.CreateAttribute("extension");
NodeAtt1.Value = "POCD_HD000040";
elmNew.SetAttributeNode(NodeAtt1);
elmRoot.AppendChild(elmNew);
xmlDoc.Save(strFilename);
XmlDocument myXmlDocument = new XmlDocument();
myXmldocument.Load(@"c:\list.xml");
XmlElement elem1;
elem1 = myXmldocument.DocumentElement;
if (elem1.Name == "ClinicalDocument")
{
XmlAttribute RootAtt3 = myXmldocument.CreateAttribute("xsi:schemaLocation");
RootAtt3.Value = "urn:h17-org:v3 CDA.xsd";
elem1.SetAttributeNode(RootAtt3);
myXmldocument.AppendChild(elem1);
}
myXmldocument.Save(@"c:\list.xml");
// Open the Makes.xml file
XmlDocument docXMLFile = new XmlDocument();
docXMLFile.Load(@"c:\list.xml");
// Get the root node so we can explore its children
XmlElement nodRoot = docXMLFile.DocumentElement;
// Store all the values of the elements in a string
string allMyChildren = nodRoot.InnerText;
Response.Write(allMyChildren);
}
// Deserialization
//serialXml newList;
//TextReader r = new StreamReader(@"c:\list.xml");
//newList = (serialXml)s.Deserialize®;
//Response.Write(newList.Items[0].name.ToString());
//Response.Write("\n");
//Response.Write("\n");
//Response.Write(newList.Items[0].price.ToString());
//r.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
void WriteError(string str)
{
outputBox.Text = str;
}
static string GetXmlString(string strFile)
{
// Load the xml file into XmlDocument object.
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(strFile);
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
}
// Now create StringWriter object to get data from xml document.
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
}
}
Now my problem is I need to validate the generated xml file using some xsd file and display the xml file as document in the output window.
Please anyone help me regarding this issue..
Thanks in advance...

New Topic/Question
Reply




MultiQuote





|