C# 如何在C中创建嵌套XML#
我想创建嵌套的xml,以便结果如下所示:C# 如何在C中创建嵌套XML#,c#,C#,我想创建嵌套的xml,以便结果如下所示: <?xml version=\"1.0\" encoding=\"UTF-8\"?><TASKLOADLOG> <PERSON> <EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR> </PERSON> <PERSON> <EMAIL>d
<?xml version=\"1.0\" encoding=\"UTF-8\"?><TASKLOADLOG>
<PERSON>
<EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR>
</PERSON>
<PERSON>
<EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR>
</PERSON>
<PERSON>
<EMAIL>data</EMAIL><LOADED>OK</LOADED><LOADERROR>ABC</LOADERROR>
</PERSON>
</TASKLOADLOG>"
dataOKABC
dataOKABC
dataOKABC
"
我写了下面的代码,它在一个循环中崩溃了
XmlDocument XmlResponse = new XmlDocument();
XmlDeclaration xDeclare = XmlResponse.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement documentRoot = XmlResponse.DocumentElement;
XmlResponse.InsertBefore(xDeclare, documentRoot);
XmlElement el = (XmlElement)XmlResponse.AppendChild(XmlResponse.CreateElement("TASKLOADLOG"));
List<XmlElement> ls = new List<XmlElement>();
for (int i = 0; i < 3; i++)
{
ls[i].AppendChild(XmlResponse.CreateElement("EMAIL")).InnerText = "data";
ls[i].AppendChild(XmlResponse.CreateElement("LOADED")).InnerText = "OK";
ls[i].AppendChild(XmlResponse.CreateElement("LOADERROR")).InnerText = "ABC";
}
MessageBox.Show(XmlResponse.OuterXml);
XmlDocument XmlResponse=new XmlDocument();
XmlDeclaration xDeclare=XmlResponse.CreateXmlDeclaration(“1.0”,“UTF-8”,null);
XmlElement documentRoot=XmlResponse.DocumentElement;
InsertBefore(xDeclare,documentRoot);
XmlElement el=(XmlElement)XmlResponse.AppendChild(XmlResponse.CreateElement(“TASKLOADLOG”);
列表ls=新列表();
对于(int i=0;i<3;i++)
{
ls[i].AppendChild(XmlResponse.CreateElement(“电子邮件”)).InnerText=“数据”;
ls[i].AppendChild(XmlResponse.CreateElement(“已加载”)).InnerText=“确定”;
ls[i].AppendChild(XmlResponse.CreateElement(“LOADERROR”)).InnerText=“ABC”;
}
Show(XmlResponse.OuterXml);
我现在不知道如何定义
PERSON
我需要编写什么来修复我的代码?问题很简单,你没有创建PERSON节点。Email、Loaded和LoadError应该是PERSON节点的子节点
编辑:
为了让您知道,您甚至可以对试图生成的XML使用类序列化
例如:
[Serializable]
public class Person
{
[XmlAttribute]
public string Email { get; set; }
public string Loaded { get; set; }
public string LoadError{ get; set; }
}
Person p = new Person
{
Email = "abc",
Loaded = "abc";
LoadError = "abc"
};
new XmlSerializer(typeof(Person)).Serialize(Console.Out, Person);
您的代码正在崩溃,因为当列表为空时,您正在引用列表的第一个索引ls 按如下方式创建文档:
var doc = XDocument(new XElement("TASKLOADLOG"));
for (int i = 0; i < 3; i++)
doc.Root.AppendChild(new XElement("PERSON",
new XElement("EMAIL", "data"),
new XElement("LOADED", "OK"),
new XElement("LOADERROR", "ABC")
)));
}
var doc=XDocument(新XElement(“TASKLOADLOG”);
对于(int i=0;i<3;i++)
doc.Root.AppendChild(新的XElement(“PERSON”),
新XElement(“电子邮件”、“数据”),
新元素(“已加载”、“正常”),
新XElement(“加载错误”、“ABC”)
)));
}
谢谢您的快速回答
我编写了以下代码,但在XmlResponse.Root.AppendChild中出错:
XmlDocument XmlResponse = new XmlDocument();
XmlDeclaration xDeclare = XmlResponse.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement documentRoot = XmlResponse.DocumentElement;
XmlResponse.InsertBefore(xDeclare, documentRoot);
XmlElement el = (XmlElement)XmlResponse.AppendChild(XmlResponse.CreateElement("TASKLOADLOG"));
//List<XmlElement> ls = new List<XmlElement>();
for (int i = 0; i < 3; i++)
{
XmlResponse.Root.AppendChild(new XElement("PERSON",
new XElement("EMAIL", "data"),
new XElement("LOADED", "OK"),
new XElement("LOADERROR", "ABC")
)));
}
MessageBox.Show(XmlResponse.OuterXml);
XmlDocument XmlResponse=new XmlDocument();
XmlDeclaration xDeclare=XmlResponse.CreateXmlDeclaration(“1.0”,“UTF-8”,null);
XmlElement documentRoot=XmlResponse.DocumentElement;
InsertBefore(xDeclare,documentRoot);
XmlElement el=(XmlElement)XmlResponse.AppendChild(XmlResponse.CreateElement(“TASKLOADLOG”);
//列表ls=新列表();
对于(int i=0;i<3;i++)
{
XmlResponse.Root.AppendChild(新的XElement(“PERSON”),
新XElement(“电子邮件”、“数据”),
新元素(“已加载”、“正常”),
新XElement(“加载错误”、“ABC”)
)));
}
Show(XmlResponse.OuterXml);
您应该解释此代码与OP代码的不同之处。