向父节点添加多个子节点
我正在尝试使用我正在收集的数据创建 XML 文件,但是对于特定的情况,我需要生成多个子标签
,所以我希望有这样的内容:-
<Feedbacks>
<Feedback>
<Name></Name>
<Surname></Surname>
<Images>
<Image></Image>
<Image></Image>
<Image></Image>
</Images>
</Feedback>
</Feedbacks>
抱歉,不知道如何在此处粘贴正确的 XML 文件,但是我想你明白了。目前我有这段正在运行的代码:-
private static void CreateFeedbackXMLFile()
{
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode Node = doc.CreateElement("Feedbacks");
doc.AppendChild(Node);
string fileName = "Feedback.xml";
string filePath = Properties.Settings.Default.DefaultFolder + "\\" + fileName;
doc.Save(filePath);
}
public static void InsertFeedback(Feedback feedback)
{
CreateFeedbackXMLFile();
string filePath = Properties.Settings.Default.DefaultFolder + "\\Feedback.xml" ;
XDocument xmlDoc = XDocument.Load(filePath);
XElement XParentElement = new XElement("Feedback");
InsertIntoXMLDoc(feedback, filePath, xmlDoc);
}
private static void InsertIntoXMLDoc(Feedback feedback, string filePath, XDocument xmlDoc)
{
xmlDoc.Element("Feedbacks").Add(new XElement("Feedback",
new XElement("Name", feedback.Name),
new XElement("Surname", feedback.Surname),
new XElement("Email", feedback.Email),
new XElement("Website", feedback.Website),
new XElement("Suggestion", feedback.Suggestion),
new XElement("Error", feedback.Error),
new XElement("MailingList", feedback.MailingList),
new XElement("Comments", feedback.Comments)
));
}
xmlDoc.Save(filePath);
}
现在我需要循环遍历 imageList 并根据我拥有的图像数量创建节点。
感谢您的帮助和时间
I am trying to create and XML file with data I am collecting, however for a particular I need multiple sub tags to be generated
So I wish to have something like this:-
<Feedbacks>
<Feedback>
<Name></Name>
<Surname></Surname>
<Images>
<Image></Image>
<Image></Image>
<Image></Image>
</Images>
</Feedback>
</Feedbacks>
Sorry do not know how to paste the correct XML file here, but I think you get the idea. At the moment I have this code that is working:-
private static void CreateFeedbackXMLFile()
{
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode Node = doc.CreateElement("Feedbacks");
doc.AppendChild(Node);
string fileName = "Feedback.xml";
string filePath = Properties.Settings.Default.DefaultFolder + "\\" + fileName;
doc.Save(filePath);
}
public static void InsertFeedback(Feedback feedback)
{
CreateFeedbackXMLFile();
string filePath = Properties.Settings.Default.DefaultFolder + "\\Feedback.xml" ;
XDocument xmlDoc = XDocument.Load(filePath);
XElement XParentElement = new XElement("Feedback");
InsertIntoXMLDoc(feedback, filePath, xmlDoc);
}
private static void InsertIntoXMLDoc(Feedback feedback, string filePath, XDocument xmlDoc)
{
xmlDoc.Element("Feedbacks").Add(new XElement("Feedback",
new XElement("Name", feedback.Name),
new XElement("Surname", feedback.Surname),
new XElement("Email", feedback.Email),
new XElement("Website", feedback.Website),
new XElement("Suggestion", feedback.Suggestion),
new XElement("Error", feedback.Error),
new XElement("MailingList", feedback.MailingList),
new XElement("Comments", feedback.Comments)
));
}
xmlDoc.Save(filePath);
}
Now I need to loop through the imageList and create nodes according to how many images I have.
Thanks for your help and time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
循环是实现您正在尝试的事情的方法。事实上,没有“无循环”的方法可以实现这一点。不过,您可以将循环伪装成
LINQ
查询,如下所示:当然,您需要将
myImageList
替换为实际的图像集合。请注意,如果您有ImageList
控件,则实际集合不是控件本身,而是其Images
属性。此外,在
...
上,您需要将用于从图像创建每个节点的任何逻辑(使用自动类型的局部变量img
来请参阅每个节点的相应图像)。Looping is the way to go for what you are trying. In fact, there is no "loopless" way to achieve that. You can, however, disguise the loop as a
LINQ
query, with something like this:Of course, you'll need to replace
myImageList
with your actual collection of images. Note that if you have anImageList
control, the actual collection is not the control itself, but itsImages
property.Also, on the
...
, you'll need to put whatever logics you are using to create each node from an image (using the auto-typed local variableimg
to refer to the appropriate image for each node).