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>
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.