XML代码重复最后标记(错误)

时间:2016-03-20 13:58:36

标签: asp.net c#-4.0

我需要你的帮助朋友 我编写了一个代码,它将从SQL TABLE中提取记录并使用数据集将其写入XML,但是在将XML从日期变化从dd / mm / yyyy写入 yyyy-mm-ddT00后,我遇到了日期转换挑战: 00:00 + 00 ,在纠正此过程中,我发现此代码为www.c-sharpcorner.com

 //XMLTextReader
//Reads the Xml data generated by DataSet
XmlTextReader XTReader = new XmlTextReader(ds1.GetXml(),
XmlNodeType.Element, null);

  //XMLTextWriter
  //To write data into xml file
  XmlTextWriter XTWriter = new XmlTextWriter(directoryPath + Tfilename.Trim() + ".xml", Encoding.UTF8);
 XTWriter.WriteStartDocument();
 string fieldName = "";
while (XTReader.Read())
{
switch (XTReader.NodeType)
{
//Check for the Node Name
case XmlNodeType.Element:
XTWriter.WriteStartElement(XTReader.Name);
fieldName = XTReader.Name;
 break;
//Check for the Node Value
 case XmlNodeType.Text:
//If the NodeName is PubDate
string Fname = "";

 foreach (string mList in mFieldname)
 {
  Fname = mList.ToString().ToLower();
  if (fieldName.ToLower() == Fname)
   break;
  }

   if (fieldName.ToLower() == Fname)
     //if (fieldName.ToLower() == SearchDate.ToLower())
    {

        DateTime dt = DateTime.Parse(XTReader.Value.ToString());

   //Here the DateTime can be changed to any format as
   //MM-dd-yyyy or dd-MM-yyyy or .ToShortDateString() ...

  XTWriter.WriteString(dt.ToString("dd/MM/yyyy"));
   }
    else
  XTWriter.WriteString(XTReader.Value);
  // break;
//}
    break;
   case XmlNodeType.EndElement:
   XTWriter.WriteEndElement();
    break;
   } //switch
    } // while

    XTWriter.Close();

此代码在将日期转换为英国 - 英国格式时运行良好,现在的问题是,它正在将XML文件从垂直方向更改为水平方式,使其难以阅读。贝娄是结果:

   <?xml version="1.0" encoding="utf-8"?><NewDataSet> <PatientDemographics_Record><Lastname>Hidden</Lastname>   <Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>263</RNL_SerialNO><Gender>Male</Gender><DateofBirth>11/01/1976</DateofBirth><AGE>40</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>ENUGU</State_OfResidence><State_OfOrigin>ENUGU</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>02371</PatientID><DateEnrolled>28/05/2015</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>01/07/2015</ART_Start_Date><ptn_pk>5988</ptn_pk></WardVillageTown_OfResidence><PatientDemographics_Record><Lastname>Hidden</Lastname><Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>574</RNL_SerialNO><Gender>Male</Gender><DateofBirth>11/04/1979</DateofBirth><AGE>36</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence>AWKUNTO</WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>ENUGU</State_OfResidence><State_OfOrigin>ENUGU</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>02348</PatientID><DateEnrolled>28/04/2015</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>24/11/2015</ART_Start_Date><ptn_pk>5916</ptn_pk></PatientDemographics_Record><PatientDemographics_Record><Lastname>Hidden</Lastname><Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>784</RNL_SerialNO><Gender>Female</Gender><DateofBirth>01/03/1990</DateofBirth><AGE>26</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>ENUGU</State_OfResidence><State_OfOrigin>ENUGU</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>02287</PatientID><DateEnrolled>17/02/2015</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>20/10/2015</ART_Start_Date><ptn_pk>6470</ptn_pk></WardVillageTown_OfResidence><PatientDemographics_Record><Lastname>Hidden</Lastname><Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>842</RNL_SerialNO><Gender>Male</Gender><DateofBirth>15/05/1985</DateofBirth><AGE>30</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence>ATANI AROCHUKWU</WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>IMO</State_OfResidence><State_OfOrigin>IMO</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>01549</PatientID><DateEnrolled>01/11/2011</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>21/07/2015</ART_Start_Date><ptn_pk>4690</ptn_pk></PatientDemographics_Record></PatientDemographics_Record></PatientDemographics_Record></NewDataSet> 

1 个答案:

答案 0 :(得分:0)

尝试设置XmlTextWriter的格式和缩进属性。

https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.formatting(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.indentation(v=vs.110).aspx

在您的情况下,请在创建XmlTextWriter后添加这些行:

 XTWriter.Formatting = Formatting.Indented;
 XTWriter.Indentation = 4;