C# 如果没有序列化工具,如何将对象转换为XML
我需要将此类转换为XML文件,但由于某些原因,我无法使用.NET序列化库。有没有什么方法可以在没有这些工具的情况下转换这个类C# 如果没有序列化工具,如何将对象转换为XML,c#,xml,object,C#,Xml,Object,我需要将此类转换为XML文件,但由于某些原因,我无法使用.NET序列化库。有没有什么方法可以在没有这些工具的情况下转换这个类 public class Product { public int Id { get; set; } public string Title { get; set; } public string Desc { get; set; } public float Price { get; set; } public Category
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public string Desc { get; set; }
public float Price { get; set; }
public Category Category { get; set; }
}
我了解XmlSerializer
,并且已经尝试了这个方法并得到了结果,但我需要在没有XmlSerializer
的情况下执行此操作:
Product product = new Product();
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(product.GetType());
serializer.Serialize(stringwriter, product);
return stringwriter.ToString();
您只需使用
StringBuilder
并连接字符串:
StringBuilder builder = new StringBuilder();
然后:
builder.AppendFormat("<instance property=\"{0}\" />", instance.Property);
builder.AppendFormat(“,instance.Property”);
如果您需要支持循环引用场景,其中:Product=>Category=>Product,那么您需要使用可以为每个实例创建唯一运行时标识的机制,这样您就不会多次序列化同一对象(请检查)
希望有帮助
您可以根据需要尝试一个抽象类,例如
尝试这样的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Product product = new Product()
{
id = 123,
title = "A Long Way Home",
desc = "Welcome",
price = 456.78F,
category = new Category() { name = "John" }
};
XElement xProduct = new XElement("Product", new object[] {
new XElement("Id", product.id),
new XElement("Title", product.title),
new XElement("Description", product.desc),
new XElement("Price", product.price),
new XElement("Category", product.category)
});
}
}
public class Product
{
public int id { get; set; }
public string title { get; set; }
public string desc { get; set; }
public float price { get; set; }
public Category category { get; set; }
}
public class Category
{
public string name { get; set; }
}
}
如果我们不知道阻止您使用标准.net功能的“某些原因”是什么?为什么您不能使用
XmlSerializer
?Xml-是文本格式,您肯定可以将一些文本写入文件(包含Xml标记)。您可以使用什么?或者?是的,这是一种通过使用XDocument类来实现的方法。如果我们为您实现它,就可以了?:)这几乎不是你自己的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Product product = new Product()
{
id = 123,
title = "A Long Way Home",
desc = "Welcome",
price = 456.78F,
category = new Category() { name = "John" }
};
XElement xProduct = new XElement("Product", new object[] {
new XElement("Id", product.id),
new XElement("Title", product.title),
new XElement("Description", product.desc),
new XElement("Price", product.price),
new XElement("Category", product.category)
});
}
}
public class Product
{
public int id { get; set; }
public string title { get; set; }
public string desc { get; set; }
public float price { get; set; }
public Category category { get; set; }
}
public class Category
{
public string name { get; set; }
}
}