简体   繁体   English

C#:填充 XML 文件

[英]C#: Filling an XML File

so I already have a XML-File which got a lot of Elements, but without any value in them.所以我已经有一个 XML 文件,它有很多元素,但没有任何价值。 Now I want do insert some Value in that already existing XML File.现在我想在已经存在的 XML 文件中插入一些值。 So I created an XmlWriter and an XmlReader.所以我创建了一个 XmlWriter 和一个 XmlReader。 After, I started writing the XMLDocument and Copied everything from the Reader like this:之后,我开始编写 XMLDocument 并从 Reader 中复制所有内容,如下所示:

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.如果我就这样保留它(当然还有xmlWriter.WriteEndDocument();xmlWriter.Close();最后),那么我将拥有一个新的 XML 文件,它是我的默认文件的精确副本。

My Question now is: Is it possible to add some Values and then safe this new XML-File?我现在的问题是:是否可以添加一些值然后保护这个新的 XML 文件? 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:如果您想知道我所说的值是什么意思,我的意思是“TestUser”,如下所示:

<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):我的 XML 看起来像这样(当然更大,那只是一个小例子):

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

And I want this XML to be Copied with some added Values, For Example:我希望这个 XML 被复制一些附加值,例如:

<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)所以你可以在这里使用这个类并打开你需要记住的所需 XML 以使用一个对象并使用不同的路径保存新文件,或者只是在 _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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM