Trước khi tôi bị mọi người nói rằng người phân tích cú pháp XML không nên quan tâm nếu các phần tử trống hoặc tự đóng, có một lý do tại sao tôi không thể cho phép các phần tử XML tự đóng. Lý do là tôi đang thực sự làm việc với SGML không phải XML và SGML DTD mà tôi đang làm việc rất nghiêm ngặt và không cho phép nó.Làm thế nào tôi có thể ngăn chặn các phần tử XML trống tự đóng bằng cách sử dụng XmlDocument trong C#?
Tôi có vài nghìn tệp SGML mà tôi cần để chạy XSLT. Do đó, tôi đã phải chuyển đổi SGML thành XML tạm thời để áp dụng XSLT. Sau đó tôi đã viết một phương thức chuyển đổi chúng trở lại SGML (về cơ bản chỉ thay thế khai báo XML bằng khai báo SGML và viết lại bất kỳ khai báo thực thể nào khác như các thực thể đồ họa).
Vấn đề của tôi là sau khi chuyển đổi này trở lại SGML, khi tôi mở tệp trong trình soạn thảo SGML của tôi, các tệp không phân tích cú pháp vì các phần tử trống đã tự đóng.
Có ai biết làm thế nào tôi có thể ngừng điều này xảy ra, vui lòng khi sử dụng XmlDocument?
Các phương pháp chuyển đổi các SGML to XML và ngược lại được trình bày dưới đây
//converts the SGML file to XML – it’s during this conversion that the
//empty elements get self-closed, i think
private XmlDocument convertToXML(TextReader reader)
{
// setup SgmlReader
Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
//sgmlReader.DocType = "HTML";
sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
sgmlReader.InputStream = reader;
// create document
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.XmlResolver = null;
doc.Load(sgmlReader);
return doc;
}
// method to apply the XSLT stylesheet to the XML document
private void filterApplic(string applicFilter)
{
string stylesheet = getRequiredStylesheet(); // do this just once
if (stylesheet != "")
{
foreach (string file in FilesToConvert)
{
fileName = Path.GetFileName(file); //gets just the file name from the path
fileNameNoExt = Path.GetFileNameWithoutExtension(file);
string ext = Path.GetExtension(file);
if (ext == ".sgm")
{
try
{
publicIdentifier = getDoctype(file); // gets the sgml declaration
entitiesList = getEntitites(file); // gets the list of entities
TextReader tr = new StreamReader(file);
myDoc = convertToXML(tr);
myDoc.Save(outputFolder + "\\temp.xml");
var myXslTrans = new XslCompiledTransform();
myXslTrans.Load(stylesheet);
myXslTrans.Transform(outputFolder + "\\temp.xml", Path.Combine(outputFolder, fileNameNoExt +".xml"));
XmlDocument convertedDoc = new XmlDocument();
convertedDoc.Load(Path.Combine(outputFolder, fileNameNoExt + ".xml"));
convertToSGM(convertedDoc);
filesTransformed++;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
else
{
MessageBox.Show("The stylesheet was retured empty. Cannot perform Applicability filter.");
return;
}
MessageBox.Show("Complete! " + filesTransformed.ToString() + " files filtered for " + applicFilter);
}
//convert files back to SGML
private void convertToSGM(XmlDocument myDoc)
{
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter, settings))
{
myDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
string xmltext = stringWriter.GetStringBuilder().ToString();
xmltext = xmltext.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "<!DOCTYPE DMODULE " + publicIdentifier + ">");
xmltext = xmltext.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "<!DOCTYPE DMODULE " + publicIdentifier + ">");
if (entitiesList.Count != 0)
{
string entityListAsOne = "";
foreach (string entity in entitiesList)
{
entityListAsOne = entityListAsOne + "\r\n" + entity;
}
xmltext = xmltext.Replace("//EN\">", "//EN\" [" + entityListAsOne + "]>");
}
File.WriteAllText(Path.Combine(outputFolder, fileNameNoExt + ".sgm"), xmltext);
}
}
Câu trả lời hoàn hảo và toàn diện. Cảm ơn nhiều. Hoàn hảo. –