C#项目中的树视图包含动态级别的类别 和产品只属于一个类别
应用程序仅创建具有单级子类别的父类别
所以我们需要的是创建具有多个子类别的XML文件
代码:
private void createXmlFile(object sender, EventArgs e)
{
try
{
List<PRG.CreateXML.Category> categories = Service.Category.Instance.FindAll();
List<PRG.CreateXML.Product> products = Service.Product.Instance.FindAll();
string fileName = "Category.xml";
string filePath = Directory.GetCurrentDirectory() + "\\data\\" + fileName;
if (File.Exists(filePath))
File.Delete(filePath);
XmlTextWriter writer = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 3;
writer.WriteStartElement("data");
createXMLCategoryFirstLevel(writer, categories, products);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
createXMLProductDetails(products);
MessageBox.Show("Create XML File Success!\nThe File Address:\n" + Directory.GetCurrentDirectory() + "\\data");
}
catch (Exception ex)
{
MessageBox.Show("Error In Creat XML File. " + ex.Message);
}
}
private void createXMLCategoryFirstLevel(XmlTextWriter writer, List<PRG.CreateXML.Category> categories, List<PRG.CreateXML.Product> products)
{
try
{
foreach (PRG.CreateXML.Category category in categories)
{
if (category.ParentId == null)
{
writer.WriteStartElement("Category");
writer.WriteStartElement("ID");
writer.WriteString(category.ID.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Name");
writer.WriteString(category.Name);
writer.WriteEndElement();
writer.WriteStartElement("Image");
if (!String.IsNullOrEmpty(category.Image))
{
writer.WriteString(category.Image.Replace(Directory.GetCurrentDirectory() + "\\", ""));
}
else
{
writer.WriteString("");
}
writer.WriteEndElement();
writer.WriteStartElement("Description");
writer.WriteString(category.Description);
writer.WriteEndElement();
createXMLSubCategory(writer, categories, category.ID, products);
writer.WriteEndElement();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void createXMLSubCategory(XmlTextWriter writer, List<PRG.CreateXML.Category> categories, long parentID, List<PRG.CreateXML.Product> products)
{
try
{
foreach (PRG.CreateXML.Category category in categories)
{
if (category.ParentId == parentID)
{
writer.WriteStartElement("SubCategory");
writer.WriteStartElement("ID");
writer.WriteString(category.ID.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Name");
writer.WriteString(category.Name);
writer.WriteEndElement();
writer.WriteStartElement("Image");
if (!String.IsNullOrEmpty(category.Image))
{
writer.WriteString(category.Image.Replace(Directory.GetCurrentDirectory() + "\\", ""));
}
else
{
writer.WriteString("");
}
writer.WriteEndElement();
writer.WriteStartElement("Description");
writer.WriteString(category.Description);
writer.WriteEndElement();
writer.WriteStartElement("ParentId");
writer.WriteString(category.ParentId.ToString());
writer.WriteEndElement();
// We Added This Two9 Lines But Error occured
// createXMLSubCategory(writer, categories, category.ID, products);
// writer.WriteEndElement();
createXMLProduct(writer, products, category.ID);
writer.WriteEndElement();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void createXMLProduct(XmlTextWriter writer, List<PRG.CreateXML.Product> products, long categoryID)
{
try
{
foreach (PRG.CreateXML.Product product in products)
{
if (product.CategoryID == categoryID)
{
writer.WriteStartElement("Product");
writer.WriteStartElement("ID");
writer.WriteString(product.ID.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Name");
writer.WriteString(product.Name);
writer.WriteEndElement();
writer.WriteStartElement("CategoryID");
writer.WriteString(product.CategoryID.ToString());
writer.WriteEndElement();
writer.WriteStartElement("Image");
if (!String.IsNullOrEmpty(product.Image))
{
writer.WriteString(product.Image.Replace(Directory.GetCurrentDirectory() + "\\", ""));
}
else
{
writer.WriteString("");
}
writer.WriteEndElement();
writer.WriteStartElement("Description");
writer.WriteString(product.Description);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:0)
解决问题
createXMLSubCategory(writer, categories, category.ID, products);
// writer.WriteEndElement();