如何在C#中添加现有XML文件中的数据? [英] How to add data in existing XML file in C#?

查看:111
本文介绍了如何在C#中添加现有XML文件中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要打开的大型xml文件,需要添加< employee> < employees>中的节点现有文件中的节点。



以下是我的xml的格式。

I have a large xml file that I need to open and needs to add <employee> node inside the <employees> node in the existing file.

Below is the format of my xml.

<company>
	<employees>
		<employee>
			<id>1</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>2</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>3</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>4</id>
			<name>sa</name>
		</employee>
	</employees>
</company>







有人可以帮助我如何在不使用XDocument或XmlDocument的情况下将数据添加到现有的xml文件中。 背后的主要原因是文件太大而且我不打算将完整的文件加载到内存中并进行进一步处理。



我尝试了什么:



我正在尝试追加数据StreamWriter,但它在< company>结束后追加数据node。




Can someone help me how I can add the data into existing xml file without using XDocument or XmlDocument. Main reason behind this is file size is too large and I am not suppose to load the complete file into memory and do the further processing.

What I have tried:

I am trying to append the data StreamWriter but it is appending the data after end of <company> node.

推荐答案

嗨Nitesh,



我提供的解决方案基于C文件指针方法C#。



逻辑:



1)在流中打开文件。

2)将文件指针移动到最后一个员工节点。

3)在那里添加一个新节点。



前两点在这里完成:

Hi Nitesh,

I am providing my solution based on C file pointer approach in C#.

Logic:

1) Open the file in a stream.
2) Move the file pointer to the last employee node.
3) Add a new node there.

First two points are accomplished here:
FileStream FStream = new FileStream("Employee.xml", FileMode.Open);

byte[] ReadAByte = new byte[1];
int FilePointerOffset = 0;

while (FilePointerOffset < FStream.Length - 25)
{
    FStream.Read(ReadAByte, 0, 1);
    FilePointerOffset++;
}

我们正在将文件指针移动到EOF之前的25个字符,因为这是我们要插入新员工记录的点。结束节点< / employees>< / company>是25个字符长,包括换行符。所以在此之前我们正在插入新员工。



第三点在这里完成:

We are moving the file pointer to 25 characters ahead of the EOF as that is the point where we want to insert a new employee record. The ending nodes "</employees></company>" are 25 characters long including newlines. So before that point we are inserting the new employee.

The third point is accomplished here:

byte[] WriteSomeBytes = new byte[200];
WriteSomeBytes = Encoding.ASCII.GetBytes("\t\t<employee>" + Environment.NewLine +
                                    "\t\t\t<id>300</id>" + Environment.NewLine +
                                    "\t\t\t<name>sa</name>" + Environment.NewLine +
                                "\t\t</employee>" + Environment.NewLine +
                            "\t</employees>" + Environment.NewLine +
                        "</company>");
FStream.Write(WriteSomeBytes, 0, WriteSomeBytes.Length);
FStream.Close();

请注意,您还需要在此处包含结尾节点,因为此部分只会从插入点丢弃任何内容。请相应地修改员工ID和姓名的值。



作为逐字节读取的替代方法,您可以直接移动到插入点。因此,代替上面的代码,以下代码的工作方式相同:

Please note that you also need to include the ending nodes here as this part will merely discard anything from the point of inserting. Please modify the values of the employee id and name accordingly.

As an alternate to reading byte by byte, you can directly move to the point of inserting. So in lieu of the above code the following code works the same:

FileStream FStream = new FileStream("Employee.xml", FileMode.Open);
byte[] ReadBytes = new byte[FStream.Length - 25];
FStream.Read(ReadBytes, 0, (int)FStream.Length - 25); // No WHILE loop needed.

byte[] WriteSomeBytes = new byte[200];
WriteSomeBytes = Encoding.ASCII.GetBytes("\t\t<employee>" + Environment.NewLine +
                                    "\t\t\t<id>22200</id>" + Environment.NewLine +
                                    "\t\t\t<name>sa</name>" + Environment.NewLine +
                                "\t\t</employee>" + Environment.NewLine +
                            "\t</employees>" + Environment.NewLine +
                        "</company>");
FStream.Write(WriteSomeBytes, 0, WriteSomeBytes.Length);
FStream.Close();

问题是文件大小;正如你所提到的那样它是一个非常大的文件,所以它可能占用了很大的空间。请尝试这两种方法并查看效果。



最后,在应用此代码之前,不要忘记保留XML文件的备份。

The problem is the file size; as you mentioned it is a pretty big file, so it might use up a huge space. Please try both approaches and see the effect.

Finally, don't forget to keep a backup of the XML file before applying this code.


您无法将数据附加到XML文件。为什么?因为您附加的数据将位于文档根标记的结束标记之外,从而使XML文件的格式无效。



您必须使用XML方法,而不是文件方法,正如你在评论中已经说过的那样。
You can't append data to an XML file. Why? Because the data you append will be outside the closing tag of the root tag of the document, thereby invalidating the format of the XML file.

You MUST insert this data using XML methods, not file methods, as you've already been told in the comments.


如果你考虑一下,你的文件是:

If you think about it, your file is:
<company>
	<employees>
		<employee>
			<id>1</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>2</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>3</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>4</id>
			<name>sa</name>
		</employee>
	</employees>
</company>



并且您想添加一个新的雇工e ,结果必须是:


and you want to add a new employee, the result must be:

<company>
	<employees>
		<employee>
			<id>1</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>2</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>3</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>4</id>
			<name>sa</name>
		</employee>
		<employee>
			<id>5</id>
			<name>sa</name>
		</employee>
	</employees>
</company>



比较两个版本,您应该了解必须如何进行更改。


Compare both versions and you should understand how changes must be done.

Quote:

如何在不使用XDocument或XmlDocument的情况下将数据添加到现有的xml文件中。

how I can add the data into existing xml file without using XDocument or XmlDocument.



如果您不能使用这些xml帮助程序,则只能手动执行。

您基本上有2种可能性,要么创建新文件,要么你更新文件。

在这两种情况下,你需要确定新数据的插入位置。


If you can't use those xml helpers, you can only do it manually.
you have basically 2 possibilities, either you create a new file, either you update the file.
In both cases, you need to determine where the new data will be inserted.


这篇关于如何在C#中添加现有XML文件中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆