0

so I already have a XML-File which got a lot of Elements, but without any value in them. Now I want do insert some Value in that already existing XML File. So I created an XmlWriter and an XmlReader. After, I started writing the XMLDocument and Copied everything from the Reader like this:

xmlWriter.WriteStartDocument();
xmlWriter.WriteNode(reader, true);

If I just leave it like that (with of course the xmlWriter.WriteEndDocument(); and xmlWriter.Close(); at the end), I will then have a new XML-File which is an exacty Copy of my default one.

My Question now is: Is it possible to add some Values and then safe this new XML-File? So basically an Copy of the default one + Values.

In Case you are wondering, what I mean by Values, I mean the "TestUser" like in the following:

<User>TestUser</User>

I´ve done some research on the Internet how to do this, but sadly I couldnt find anything.

Thanks for your help!

EDIT:

My XML looks something like this (of course larger, thats just a small example):

<users>
    <user></user>
    <user></user>
</users>

And I want this XML to be Copied with some added Values, For Example:

<users>
    <user>TestUser1</user>
    <user>TestUser2</user>
</users>
Warsox
  • 63
  • 9
  • 4
    Unless you are working with massive XML documents (>100MB), it is far easier to work with `XDocument` and related. – Mitch Jan 29 '20 at 16:03
  • 1
    Microsoft has good [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.serialize?view=netframework-4.8). Use serializers to read template, fill in values, then use serializer again to write values to file – Jawad Jan 29 '20 at 16:27
  • @Mitch can you show me an Example? I´ve tried that now, and I am once more at the same point. I got the "Copy" of the XML but I dont know how I can add those Values to those Elements. – Warsox Jan 29 '20 at 16:39
  • https://www.c-sharpcorner.com/article/xml-manipulation-in-c-sharp/ Looks fairly complete, but some searching using XElement or XDocument should be elucidating. – Mitch Jan 29 '20 at 17:48
  • Assuming you can't load the entire XML document into memory, take a look at [Combining the XmlReader and XmlWriter classes for simple streaming transformations](https://web.archive.org/web/20160214201608/http://blogs.msdn.com/b/mfussell/archive/2005/02/12/371546.aspx) and two questions with answers that use that article: [Read xml, update it and write to stream again without loading it all](https://stackoverflow.com/q/28355317/3744182) and [Automating replacing tables from external files](https://stackoverflow.com/q/28891440/3744182). – dbc Jan 29 '20 at 18:58
  • But first be sure that it can't be loaded successfully. Very large XML files can often be loaded successfully by deserialization with `XmlSerializer`, e.g. as in [How to Deserialize XML document](https://stackoverflow.com/q/364253/3744182) + [Generate C# class from XML](https://stackoverflow.com/a/4203551/3744182). – dbc Jan 29 '20 at 19:01
  • 1
    *Mitch can you show me an Example?* - to do that we need a complete XML sample -- i.e. a [mcve] -- showing where you are stuck, that we could paste into a console app or [fiddle](https://dotnetfiddle.net/) and run. – dbc Jan 29 '20 at 19:03
  • @dbc I just Edited the Question with a small XML sample. – Warsox Jan 30 '20 at 08:22
  • I´ve found it out! I can go through my Childs with "XmlDocument.FirstChild" and then set the Text via "XmlNode.InnerText". – Warsox Jan 30 '20 at 10:00

1 Answers1

-1

so you could use this class here and open the desired XML that you need keep in mind to use a object and save the new file with a diferent path or just rename the file in the _Serialize(string filePath, T object)

    public static StreamReader _StreamReader(string filePath)
    {
        try
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new InvalidOperationException();
            }

            return new StreamReader(filePath);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static void _Serialize<T>(string filePath, T object)
    {
        try
        {
            var xmlSerializer = new XmlSerializer(object.GetType());
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                xmlSerializer.Serialize(fileStream, object);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static T _UnSerialize<T>(StreamReader streamReader)
    {
        try
        {
            T deserializedObject = default(T);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            deserializedObject = (T)xmlSerializer.Deserialize(streamReader);
            streamReader.Dispose();
            return deserializedObject;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }