向父节点添加多个子节点

发布于 2024-11-01 17:46:40 字数 2273 浏览 3 评论 0原文

我正在尝试使用我正在收集的数据创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜`诱少女 2024-11-08 17:46:40

循环是实现您正在尝试的事情的方法。事实上,没有“无循环”的方法可以实现这一点。不过,您可以将循环伪装成 LINQ 查询,如下所示:

xmlDoc.Element("Feedbacks").Add(
    /* All the elements before your image list */
    XElement("images", 
        from img in myImageList select new XElement(...)
    )
    /* All the elements after your image list, preceeded by a comma */
);

当然,您需要将 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:

xmlDoc.Element("Feedbacks").Add(
    /* All the elements before your image list */
    XElement("images", 
        from img in myImageList select new XElement(...)
    )
    /* All the elements after your image list, preceeded by a comma */
);

Of course, you'll need to replace myImageList with your actual collection of images. Note that if you have an ImageList control, the actual collection is not the control itself, but its Images 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 variable img to refer to the appropriate image for each node).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文