-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLFile.cs
More file actions
156 lines (131 loc) · 5.03 KB
/
XMLFile.cs
File metadata and controls
156 lines (131 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadAndWriteFile
{
public static class XMLFile
{
/// <summary>
/// using async method to write string xml to local
/// </summary>
/// <param name="contents">xml content</param>
/// <returns>Task</returns>
public static async Task WriteXmlOneAsync(string contents)
{
byte[] encodedXml = Encoding.UTF8.GetBytes(contents);
using (FileStream fileStream = new FileStream(fileFullName,
FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite,
bufferSize: 8192, useAsync: true))
{
await fileStream.WriteAsync(encodedXml, 0, encodedXml.Length);
}
}
/// <summary>
/// Read xml from local
/// </summary>
/// <returns>string xml content</returns>
public static async Task<string> ReadXmlOneAsync()
{
using (FileStream fileStream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true))
{
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
string text = Encoding.UTF8.GetString(buffer, 0, numRead);
sb.Append(text);
}
return sb.ToString();
}
}
/// <summary>
/// Read xml from local
/// </summary>
/// <returns>byte[] xml content</returns>
public static async Task<byte[]> ReadXmlTwoAsync()
{
using (FileStream fileStream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true))
{
var arrays = new Stack<byte[]>();
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
arrays.Push(buffer);
}
var arrayList = arrays.ToArray<byte[]>();
return CombineByteArrays(arrayList);
}
}
/// <summary>
/// Read xml from local
/// </summary>
/// <returns>byte[] xml content</returns>
public static async Task<byte[]> ReadXmlThreeAsync()
{
using (FileStream fileStream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true))
{
IEnumerable<byte> myArray = null;
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
myArray = myArray.Concat(buffer);
}
var arrayList = myArray.ToArray();
return arrayList;
}
}
/// <summary>
/// Read xml file from local
/// </summary>
/// <returns>byte[] xml content</returns>
public static async Task<byte[]> ReadXmlFourAsync()
{
byte[] xmlContent = null;
using (FileStream readFileStream = File.Open(fileFullName, FileMode.Open))
{
xmlContent = new byte[readFileStream.Length];
await readFileStream.ReadAsync(xmlContent, 0, (int)readFileStream.Length);
}
return xmlContent;
}
/// <summary>
/// write xml to local
/// </summary>
/// <param name="contents">xml content</param>
/// <returns>Task</returns>
public static async Task WriteXmlTwoAsync(string contents)
{
byte[] fileContents = Encoding.UTF8.GetBytes(contents);
using (FileStream writeFileStream = File.OpenWrite(fileFullName))
{
await writeFileStream.WriteAsync(fileContents, 0, fileContents.Length);
}
}
/// <summary>
/// Combine some byte array to one array
/// </summary>
/// <param name="arrays">one or more byte array's arrays</param>
/// <returns>byte array</returns>
public static byte[] CombineByteArrays(params byte[][] arrays)
{
byte[] returnArray = new byte[arrays.Sum(x => x.Length)];
int offset = 0;
foreach (byte[] item in arrays)
{
Buffer.BlockCopy(item, 0, returnArray, offset, item.Length);
offset += item.Length;
}
return returnArray;
}
/// <summary>
/// xml'file name that contains file path
/// </summary>
static string fileFullName = AppDomain.CurrentDomain.BaseDirectory + "test.xml";
}
}