0

I need to create an XML document like this:

<Root>
  <Data>
    <Name>Name1</Name>
    <Surname>Surname1</Surname>
    <Gender>M</Gender>
  </Data>
  <Data>
    <Name>Name2</Name>
    <Surname>Surname2</Surname>
    <Gender>F</Gender>
  </Data>
</Root>

I've got the Xpath of the XML Elements, so I've create the following class

class XpathFieldValue
{
    public string Xpath { get; set; }
    public string Value { get; set; }
}

and then the following method

public static void CreateXml()
    {
        List<XpathFieldValue> fieldValues = new List<XpathFieldValue> {
        new XpathFieldValue{ Xpath="/Root/Data/Name", Value="Name1" },
        new XpathFieldValue{ Xpath="/Root/Data/Surname", Value="Surname1" },
        new XpathFieldValue{ Xpath="/Root/Data/Gender", Value="M"},
        new XpathFieldValue{ Xpath="/Root/Data/Name", Value="Name2" },
        new XpathFieldValue{ Xpath="/Root/Data/Surname", Value="Surname2" },
        new XpathFieldValue{ Xpath="/Root/Data/Gender", Value="F"}
        };
        XmlDocument document = new XmlDocument();
        document.LoadXml("<Root/>");
        foreach (XpathFieldValue fieldValue in fieldValues)
        {
            Set(document, fieldValue.Xpath, fieldValue.Value);
        }
        document.Save(@"C:\Temp\xmlDocOut.xml");
    }

I've copyed the Set method from here: link

But when I run it it creates only the last parth of XML

<Root>
  <Data>
    <Name>Name2</Name>
    <Surname>Surname2</Surname>
    <Gender>F</Gender>
  </Data>
</Root>

Can anyone help me?

Bob
  • 3
  • 1

1 Answers1

1

XPath isn't really intended to generate documents but to access em.

Another (probably better) approach would be to serialize it. But, since the question requires the fields of xml to be variable, serialization isn't a viable way. i STRONGLY advise you to change this requirement and you'll see why in the following example:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var writer = new System.IO.StreamWriter(@"C:\Users\luigi.trabacchin\Desktop\asd.xml"))
            {
                var doc = new System.Xml.XmlDocument();
                var root = doc.CreateElement("Root");
                doc.AppendChild(root);
                for (var i = 0; i <= 1; i++)
                {
                    var dataNode = doc.CreateElement("Data");
                    root.AppendChild(dataNode);
                    {
                        var node = doc.CreateElement("Name");
                        dataNode.AppendChild(node);
                        var text = doc.CreateTextNode($"Name {i}");
                        node.AppendChild(text);
                    }
                    {
                        var node = doc.CreateElement("Surname");
                        dataNode.AppendChild(node);
                        var text = doc.CreateTextNode($"Surname {i}");
                        node.AppendChild(text);
                    }
                    {
                        var node = doc.CreateElement("Gender");
                        dataNode.AppendChild(node);
                        var text = doc.CreateTextNode(i %2 == 0 ? "M" : "F");
                        node.AppendChild(text);
                    }
                }

                doc.Save(writer);
            }
            Console.WriteLine("Hello World!");
        }
    }
}

It quickly becomes very tedious and hard to work with. Still i hope this answer your question. Next time, maybe, state all the requirements directly in the question!

L.Trabacchin
  • 1,538
  • 1
  • 17
  • 34
  • Ok, but if I add the index the xml result will be Name1 Surname1 M Name2 Surname2 F and I don't need the index in the node – Bob May 08 '20 at 12:24
  • uhm... i fear this isn't a good approach, why do you need xpath at all ? won't it be easier to create a class to serialzie ? – L.Trabacchin May 08 '20 at 12:49
  • I can't create a class because the element name ot output xml I create can be change from month to month and the user who is using the program can change the element name – Bob May 08 '20 at 14:58
  • ohh i see, bad design so xD i'll answer one more time then – L.Trabacchin May 08 '20 at 16:06