问题描述
大家好!
我一直在努力避免在这里发布此问题,因为我更愿意自行解决,但我很困惑.
我的程序中已经有一个将数据编译成XMLDocument的类,该类可以正常工作,没有任何问题,但是采用相同的代码并将其扩展为更复杂的xml模式,导致格式化问题比XML阅读器更为奇怪. 无法理解.
现在,我已经将此代码重写了4到5次,可以肯定是由于某种原因导致的,但是我似乎找不到它.以前有没有人处理过类似的事情?我在网上进行了一些搜索,发现几乎没有发现...
这些数据都是虚构的,因此我将其发布在下面.
这是我的ResultString(XML)返回的内容:
Hi everyone!
I've been trying to avoid posting this question on here as I prefer to figure it out on my own if at all possible, but I am just stumped.
I already have within my program a class that compiles data into an XMLDocument that works perfectly fine with no issue, however taking the same code and expanding it for a more complex xml schema has resulted in rather bizarre formatting issues that XML readers are unable to comprehend.
I have rewritten this code 4 or 5 times now, certain that there was something that I was doing to cause this, but I can't seem to find it. Has anyone dealt with anything like this before? I did some searching online that found virtually nothing...
This data is all ficticious, so I'll just post it below.
This is what my ResultString (The XML) returns:
<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<Data>\r\n <Software>\r\n <Application>ProjectDesk</Application>\r\n <Version>1.0.10.5</Version>\r\n </Software>\r\n <Summary>\r\n <CID>1</CID>\r\n <JID>83</JID>\r\n <CCompany />\r\n <CFName />\r\n <CLName />\r\n <JobType>Radon</JobType>\r\n <Total>$ 2,345.75</Total>\r\n <Tax>$ 0.00</Tax>\r\n <SubTotal>$ 2,345.75</SubTotal>\r\n <ItemCount>8</ItemCount>\r\n <DateCreated>2/21/2017 3:35:16 PM</DateCreated>\r\n <CreatedBy>1</CreatedBy>\r\n <DateUpdated>2/21/2017 3:35:14 PM</DateUpdated>\r\n <UpdatedBy />\r\n </Summary>\r\n <Estimate>\r\n <Title />\r\n <Description />\r\n <Rooms>\r\n <Room>\r\n <Floor />\r\n <Room>Job</Room>\r\n <WidthString>0' 0\"</WidthString>\r\n <LengthString>0' 0\"</LengthString>\r\n <HeightString>0' 0\"</HeightString>\r\n <WidthInt>0</WidthInt>\r\n <LengthInt>0</LengthInt>\r\n <HeightInt>0</HeightInt>\r\n <Offsets />\r\n <Openings />\r\n <Items>\r\n <Item>\r\n <ID>1</ID>\r\n <Category>RDN</Category>\r\n <Code>PBSC</Code>\r\n <Type>Estimate</Type>\r\n <Title>Test Title</Title>\r\n <Description />\r\n <PriceString>0.00</PriceString>\r\n <Price>0.00</Price>\r\n <Unit>JOB</Unit>\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0.00</TotalString>\r\n <Total>0.00</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Selected</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>2</ID>\r\n <Category>RDN</Category>\r\n <Code>PELT</Code>\r\n <Type>Estimate</Type>\r\n <Title>Test Title</Title>\r\n <Description />\r\n <PriceString>0.00</PriceString>\r\n <Price>0.00</Price>\r\n <Unit>JOB</Unit>\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0.00</TotalString>\r\n <Total>0.00</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Selected</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>3</ID>\r\n <Category>RDN</Category>\r\n <Code>FBSC</Code>\r\n <Type>Estimate</Type>\r\n <Title>Test Title</Title>\r\n <Description />\r\n <PriceString>0.00</PriceString>\r\n <Price>0.00</Price>\r\n <Unit>EA</Unit>\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0.00</TotalString>\r\n <Total>0.00</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Selected</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>13</ID>\r\n <Category>RDN</Category>\r\n <Code />\r\n <Type>Note</Type>\r\n <Title>Note</Title>\r\n <Description>Test Note</Description>\r\n <PriceString>0.00</PriceString>\r\n <Price>0</Price>\r\n <Unit />\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0</TotalString>\r\n <Total>0</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Mandatory</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>14</ID>\r\n <Category>RDN</Category>\r\n <Code />\r\n <Type>Note</Type>\r\n <Title>Note</Title>\r\n <Description>Test Note</Description>\r\n <PriceString>0.00</PriceString>\r\n <Price>0</Price>\r\n <Unit />\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0</TotalString>\r\n <Total>0</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Mandatory</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>15</ID>\r\n <Category>RDN</Category>\r\n <Code />\r\n <Type>Note</Type>\r\n <Title>Note</Title>\r\n <Description>Test Note</Description>\r\n <PriceString>0.00</PriceString>\r\n <Price>0</Price>\r\n <Unit />\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0</TotalString>\r\n <Total>0</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Mandatory</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>16</ID>\r\n <Category>RDN</Category>\r\n <Code />\r\n <Type>Note</Type>\r\n <Title>Note</Title>\r\n <Description>Test Note</Description>\r\n <PriceString>0.00</PriceString>\r\n <Price>0</Price>\r\n <Unit />\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0</TotalString>\r\n <Total>0</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Mandatory</TemplateStatus>\r\n </Item>\r\n <Item>\r\n <ID>17</ID>\r\n <Category>RDN</Category>\r\n <Code />\r\n <Type>Note</Type>\r\n <Title>Note</Title>\r\n <Description>Test Note</Description>\r\n <PriceString>0.00</PriceString>\r\n <Price>0</Price>\r\n <Unit />\r\n <Image />\r\n <MinimumCharge>0.00</MinimumCharge>\r\n <Materials />\r\n <Tax>$ 0.00</Tax>\r\n <Selected>True</Selected>\r\n <TotalString>0</TotalString>\r\n <Total>0</Total>\r\n <TemplateType>Radon</TemplateType>\r\n <TemplateStatus>Mandatory</TemplateStatus>\r\n </Item>\r\n </Items>\r\n </Room>\r\n </Rooms>\r\n </Estimate>\r\n</Data>
格式不仅在整个过程中都包含\ r \ n,而且声明中还包含多个\字符...?
这是我的代码:
Not only does the formatting include \r\n throughout, but the declaration includes several \ characters...?
This is my code:
public static string GenerateEstimate(DataTable _customer, DataTable _rooms, DataTable _offsets, DataTable _openings, DataTable _items, string _currentEstimate)
{
string ResultString = "";
//Software
string Application = "Demo";
string ApplicatinVersion = CAssembly.AssemblyVersion.ToString();
//Summary
DataRow SummaryRow = _customer.Rows[0];
string CID = SummaryRow["CID"].ToString();
string JID = SummaryRow["JID"].ToString();
string CCompany = SummaryRow["CCompany"].ToString();
string CFName = SummaryRow["CFName"].ToString();
string CLName = SummaryRow["CLName"].ToString();
string JobType = SummaryRow["JobType"].ToString();
string Total = SummaryRow["Total"].ToString();
string Tax = SummaryRow["Tax"].ToString();
string SubTotal = SummaryRow["SubTotal"].ToString();
string ItemCount = SummaryRow["ItemCount"].ToString();
string DateCreated = SummaryRow["DateCreated"].ToString();
string CreatedBy = SummaryRow["CreatedBy"].ToString();
string DateUpdated = SummaryRow["DateUpdated"].ToString();
string UpdatedBy = SummaryRow["UpdatedBy"].ToString();
//Estimate
string EstimateTitleString = "";
string EstimateDescriptionString = "";
if (_currentEstimate == "")
{
try
{
//Format xml file.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder clearXml = new StringBuilder();
XmlWriter writer = XmlWriter.Create(clearXml, settings);
//Build body of xml file.
writer.WriteStartDocument();
writer.WriteStartElement("Data");
//Software
writer.WriteStartElement("Software");
writer.WriteElementString("Application", Application);
writer.WriteElementString("Version", ApplicatinVersion);
writer.WriteEndElement();
//Summary
writer.WriteStartElement("Summary");
writer.WriteElementString("CID", CID);
writer.WriteElementString("JID", JID);
writer.WriteElementString("CCompany", CCompany);
writer.WriteElementString("CFName", CFName);
writer.WriteElementString("CLName", CLName);
writer.WriteElementString("JobType", JobType);
writer.WriteElementString("Total", Total);
writer.WriteElementString("Tax", Tax);
writer.WriteElementString("SubTotal", SubTotal);
writer.WriteElementString("ItemCount", ItemCount);
writer.WriteElementString("DateCreated", DateCreated);
writer.WriteElementString("CreatedBy", CreatedBy);
writer.WriteElementString("DateUpdated", DateUpdated);
writer.WriteElementString("UpdatedBy", UpdatedBy);
writer.WriteEndElement();
//Estimate
writer.WriteStartElement("Estimate");
writer.WriteElementString("Title", EstimateTitleString);
writer.WriteElementString("Description", EstimateDescriptionString);
writer.WriteStartElement("Rooms");
foreach (DataRow room in _rooms.Rows)
{
string Floor = room["Floor"].ToString();
string Room = room["Room"].ToString();
string WidthString = room["Width"].ToString();
int Width = Convert.ToInt32(WidthString);
string LengthString = room["Length"].ToString();
int Length = Convert.ToInt32(LengthString);
string HeightString = room["Height"].ToString();
int Height = Convert.ToInt32(HeightString);
//Format Dimensions
WidthString = CMath.GetFormattedFeetAndInchesFromInches(Width);
LengthString = CMath.GetFormattedFeetAndInchesFromInches(Length);
HeightString = CMath.GetFormattedFeetAndInchesFromInches(Height);
//Rooms
writer.WriteStartElement("Room");
writer.WriteElementString("Floor", Floor);
writer.WriteElementString("Room", Room);
writer.WriteElementString("WidthString", WidthString);
writer.WriteElementString("LengthString", LengthString);
writer.WriteElementString("HeightString", HeightString);
writer.WriteElementString("WidthInt", Width.ToString());
writer.WriteElementString("LengthInt", Length.ToString());
writer.WriteElementString("HeightInt", Height.ToString());
//Offsets
writer.WriteStartElement("Offsets");
if (_offsets.Rows.Count > 0)
{
foreach (DataRow offset in _offsets.Rows)
{
if (offset["Room"].ToString() == Room)
{
string Offset = offset["Offset"].ToString();
string Type = offset["Type"].ToString();
string OffsetRoom = offset["Room"].ToString();
string OWidthString = offset["Width"].ToString();
int OWidth = Convert.ToInt32(WidthString);
string ODepthString = offset["Depth"].ToString();
int ODepth = Convert.ToInt32(LengthString);
string OHeightString = offset["Height"].ToString();
int OHeight = Convert.ToInt32(HeightString);
//Format Dimensions
OWidthString = CMath.GetFormattedFeetAndInchesFromInches(OWidth);
ODepthString = CMath.GetFormattedFeetAndInchesFromInches(ODepth);
OHeightString = CMath.GetFormattedFeetAndInchesFromInches(OHeight);
writer.WriteStartElement("Offset");
writer.WriteElementString("Offset", Offset);
writer.WriteElementString("Type", Type);
writer.WriteElementString("OffsetRoom", OffsetRoom);
writer.WriteElementString("WidthString", OWidthString);
writer.WriteElementString("DepthString", ODepthString);
writer.WriteElementString("HeightString", OHeightString);
writer.WriteElementString("WidthInt", OWidth.ToString());
writer.WriteElementString("DepthInt", ODepth.ToString());
writer.WriteElementString("HeightInt", OHeight.ToString());
writer.WriteEndElement();
}
}
}
writer.WriteEndElement();
//Openings
writer.WriteStartElement("Openings");
if (_openings.Rows.Count > 0)
{
foreach (DataRow opening in _openings.Rows)
{
if (opening["Room"].ToString() == Room)
{
string Opening = opening["Opening"].ToString();
string Type = opening["Type"].ToString();
string OpeningRoom = opening["Room"].ToString();
string ToOffset = opening["ToOffset"].ToString();
string OWidthString = opening["Width"].ToString();
int OWidth = Convert.ToInt32(WidthString);
string OHeightString = opening["Height"].ToString();
int OHeight = Convert.ToInt32(HeightString);
//Format Dimensions
OWidthString = CMath.GetFormattedFeetAndInchesFromInches(OWidth);
OHeightString = CMath.GetFormattedFeetAndInchesFromInches(OHeight);
writer.WriteStartElement("Opening");
writer.WriteElementString("Opening", Opening);
writer.WriteElementString("Type", Type);
writer.WriteElementString("OffsetRoom", OpeningRoom);
writer.WriteElementString("ToOffset", ToOffset);
writer.WriteElementString("WidthString", OWidthString);
writer.WriteElementString("HeightString", OHeightString);
writer.WriteElementString("WidthInt", OWidth.ToString());
writer.WriteElementString("HeightInt", OHeight.ToString());
writer.WriteEndElement();
}
}
}
writer.WriteEndElement();
//Items
writer.WriteStartElement("Items");
if (_items.Rows.Count > 0)
{
foreach (DataRow item in _items.Rows)
{
string ItemRoom = item["Room"].ToString();
if (ItemRoom == Room)
{
string ID = item["ID"].ToString();
string Category = item["Category"].ToString();
string Code = item["Code"].ToString();
string Type = item["Type"].ToString();
string Title = item["Title"].ToString();
string Description = item["Description"].ToString();
string PriceString = item["Price"].ToString();
double Price = Convert.ToDouble(PriceString);
string Unit = item["Unit"].ToString();
string Image = item["Image"].ToString();
string MinimumCharge = item["MinimumCharge"].ToString();
string Materials = item["Materials"].ToString();
string Taxable = item["Tax"].ToString();
string Selected = item["Selected"].ToString();
string LineTotalString = item["Total"].ToString();
double LineTotal = Convert.ToDouble(LineTotalString);
string TemplateType = item["TemplateType"].ToString();
string TemplateStatus = item["TemplateStatus"].ToString();
writer.WriteStartElement("Item");
writer.WriteElementString("ID", ID);
writer.WriteElementString("Category", Category);
writer.WriteElementString("Code", Code);
writer.WriteElementString("Type", Type);
writer.WriteElementString("Title", Title);
writer.WriteElementString("Description", Description);
writer.WriteElementString("PriceString", PriceString);
writer.WriteElementString("Price", Price.ToString());
writer.WriteElementString("Unit", Unit);
writer.WriteElementString("Image", Image);
writer.WriteElementString("MinimumCharge", MinimumCharge);
writer.WriteElementString("Materials", Materials);
writer.WriteElementString("Tax", Tax);
writer.WriteElementString("Selected", Selected);
writer.WriteElementString("TotalString", LineTotalString);
writer.WriteElementString("Total", LineTotal.ToString());
writer.WriteElementString("TemplateType", TemplateType);
writer.WriteElementString("TemplateStatus", TemplateStatus);
writer.WriteEndElement();
}
}
}
//End Items
writer.WriteEndElement();
//End Room
writer.WriteEndElement();
}
//End Rooms
writer.WriteEndElement();
//End Estimate
writer.WriteEndElement();
//End Data
writer.WriteEndElement();
//End Document
writer.WriteEndDocument();
writer.Flush();
writer.Close();
//Create an XmlDocument object
XmlDocument xmlDoc = new XmlDocument();
//Load the XML file into the XmlDocument object
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(clearXml.ToString());
ResultString = xmlDoc.InnerXml.ToString();
}
catch
{
}
}
else
{
}
return ResultString;
}
我真的很感谢任何人在这方面可以给我的帮助.我真的无法弄清楚问题是什么或什么原因造成的.
我在其他地方使用的相同(但更简单)的代码可以正常工作,没有任何问题...
非常感谢您的帮助.
I really appreciate any help anyone can give me on this. I really cannot figure out what the issue is or what would be causing this.
The identical (But simpler) code that I use elsewhere works without any issue whatsoever...
Thank you very much for your help.
推荐答案
System.Diagnostics.Trace.WriteLine(ResultString);
System.Diagnostics.Trace.WriteLine(ResultString);
您能否提供有关如何使用此方法返回的结果字符串的详细信息?
Can you provide details on how you are using the ResultString returned from this method?
这篇关于XML格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!