asked on
Severity Code Description Project File Line Suppression State
Error CS1519 Invalid token '=' in class, struct, or interface member declaration ConsoleApplication2 d:\VB\Test\ConsoleApplication2\ConsoleApplication2\Program.cs 13 Active
using System.IO;
using xml;
namespace ConsoleApplication2
{
class Program
{
private history = new history();
static void Main(string[] args)
{
string RootDir = @"C:\Users\user\AppData\Roaming\AppName\user";
if (Directory.Exists(RootDir))
{
var Files = Directory.EnumerateFiles(RootDir, "history.xml", SearchOption.AllDirectories);
foreach (string File in Files)
{
this.history += xml.history.Load(File);
}
}
}
}
}
using System.Xml.Serialization;
namespace xml
{
public class history
{
List<post> post{ get; set; } = new List<post>();
private static string xmlFile { get; set; }
public static history Load(string xmlFile)
{
XmlSerializer deserializer = new XmlSerializer(typeof(history));
using (TextReader reader = new StreamReader(xmlFile))
{
history history = (history)deserializer.Deserialize(reader);
return history;
}
}
public void Save(string xmlFile)
{
XmlSerializer serializer = new XmlSerializer(typeof(history));
using (TextWriter writer = new StreamWriter(xmlFile))
{
serializer.Serialize(writer, this);
}
}
}
public class post
{
public string name { get; set; }
public string url { get; set; }
public int id { get; set; }
public int number { get; set; }
public int imageCount { get; set; }
public int downloadedImagesCount { get; set; }
public string finished { get; set; }
}
}
<history>
<post>
<name>My Collection </name>
<url>https://example.com/hread.php?p=12345</url>
<id>12345</id>
<number>1</number>
<imageCount>132</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
...............
</History>
ASKER
class Program
{
private static history history = new history();
static void Main(string[] args)
{
string RootDir = @"C:\Users\user\AppData\Roaming\AppName\user";
if (Directory.Exists(RootDir))
{
var Files = Directory.EnumerateFiles(RootDir, "history.xml", SearchOption.AllDirectories);
foreach (string File in Files)
{
history = history.Load(File);
}
// no post after the dot
history.
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace EE_Q29135911
{
class Program
{
static List<History> history = new List<History>();
static void Main(string[] args)
{
string directory = @"C:\Users\user\AppData\Roaming\AppName\user";
if (Directory.Exists(directory))
{
var files = Directory.EnumerateFiles(directory, "history.xml", SearchOption.AllDirectories);
foreach (string file in files)
{
history.Add(History.Load(file));
}
}
}
}
class History
{
public List<Post> post { get; set; } = new List<Post>();
public static History Load(string file)
{
var deserializer = new XmlSerializer(typeof(History));
using (TextReader reader = new StreamReader(file))
{
return deserializer.Deserialize(reader) as History;
}
}
public void Save(string file)
{
var serializer = new XmlSerializer(typeof(History));
using (TextWriter writer = new StreamWriter(file))
{
serializer.Serialize(writer, this);
}
}
}
public class Post
{
public string Name { get; set; }
public string Url { get; set; }
public int Id { get; set; }
public int Number { get; set; }
public int ImageCount { get; set; }
public int DownloadedImagesCount { get; set; }
public string Finished { get; set; }
}
}
-saige-
ASKER
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=MergeXML.History is inaccessible due to its protection level. Only public types can be processed.
Source=System.Xml
StackTrace:
at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at MergeXML.History.Load(String file) in d:\VB\Test\MergeXML\MergeXML\Program.cs:line 39
at MergeXML.Program.Main(String[] args) in d:\VB\Test\MergeXML\MergeXML\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
using System.IO;
using System.Xml.Serialization;
namespace MergeXML
{
class Program
{
static List<History> history = new List<History>();
static void Main(string[] args)
{
string RootDir = @"C:\Users\user\AppData\Roaming\AppName\user";
if (Directory.Exists(RootDir))
{
var files = Directory.EnumerateFiles(RootDir, "history.xml", SearchOption.AllDirectories);
foreach (string file in files)
{
history.Add(History.Load(file));
}
}
// ###1
History.Save(RootDir + "\\history_out.xml");
}
}
class History
{
public List<Post> post { get; set; } = new List<Post>();
#region Loading/Saving
public static History Load(string file)
{
var deserializer = new XmlSerializer(typeof(History)); // ###2
using (TextReader reader = new StreamReader(file))
{
return deserializer.Deserialize(reader) as History;
}
}
internal static void Save(string file)
{
var serializer = new XmlSerializer(typeof(History));
using (TextWriter writer = new StreamWriter(file))
{
serializer.Serialize(writer, file);
}
}
#endregion
}
public class Post
{
public string name { get; set; }
public string url { get; set; }
public int id { get; set; }
public int number { get; set; }
public int imageCount { get; set; }
public int downloadedImagesCount { get; set; }
public string finished { get; set; }
}
}
using System.IO;
using System.Xml.Serialization;
namespace MergeXML
{
class Program
{
static List<History> history = new List<History>();
static void Main(string[] args)
{
string RootDir = @"C:\Users\user\AppData\Roaming\AppName\user";
if (Directory.Exists(RootDir))
{
var files = Directory.EnumerateFiles(RootDir, "history.xml", SearchOption.AllDirectories);
foreach (string file in files)
{
history.Add(History.Load(file));
}
}
// ###1
History.Save(RootDir + "\\history_out.xml");
}
}
public class History
{
public List<Post> post { get; set; } = new List<Post>();
#region Loading/Saving
public static History Load(string file)
{
var deserializer = new XmlSerializer(typeof(History)); // ###2
using (TextReader reader = new StreamReader(file))
{
return deserializer.Deserialize(reader) as History;
}
}
internal static void Save(string file)
{
var serializer = new XmlSerializer(typeof(History));
using (TextWriter writer = new StreamWriter(file))
{
serializer.Serialize(writer, file);
}
}
#endregion
}
public class Post
{
public string name { get; set; }
public string url { get; set; }
public int id { get; set; }
public int number { get; set; }
public int imageCount { get; set; }
public int downloadedImagesCount { get; set; }
public string finished { get; set; }
}
}
-saige-
ASKER
// ###1
Post post = new Post()
{
name = "Hello World",
url = "https://www.experts-exchange.com/questions/29135911/C-Merging-XML-files.html",
id= 45678,
number =7,
imageCount =99,
downloadedImagesCount = 99,
finished ="true"
};
history.Add(post);
History.Save(RootDir + "\\history_out.xml");
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'MergeXML.Post' to 'MergeXML.History' MergeXML D:\Vb\Test\MergeXML\MergeXML\Program.cs 41 Active
<?xml version="1.0" encoding="windows-1250"?>
<history xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<post>
<name>My Collection </name>
<url>https://example.com/hread.php?p=12345</url>
<id>12345</id>
<number>1</number>
<imageCount>132</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
</history>
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=There is an error in XML document (2, 2).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at MergeXML.History.Load(String file) in D:\Vb\Test\MergeXML\MergeXML\Program.cs:line 44
at MergeXML.Program.Main(String[] args) in D:\Vb\Test\MergeXML\MergeXML\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
HResult=-2146233079
Message=<history xmlns=''> was not expected.
Source=Microsoft.GeneratedCode
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderHistory.Read4_History()
InnerException:
var history = new History();
Post post = new Post()
{
name = "Hello World",
url = "https://www.experts-exchange.com/questions/29135911/C-Merging-XML-files.html",
id= 45678,
number =7,
imageCount =99,
downloadedImagesCount = 99,
finished ="true"
};
history.post.Add(post);
-saige-
ASKER
There was an error generating the XML document.
InnerException:
HResult=-2147467262
Message=Unable to cast object of type 'System.String' to type 'MergeXML.History'.
internal static void Save(string file)
{
var serializer = new XmlSerializer(typeof(History));
using (TextWriter writer = new StreamWriter(file))
{
serializer.Serialize(writer, file); // Errors here
}
}
ASKER
[XmlElement(ElementName = "name")]
public string Name { get; set; }
History history = new History();
Post post = new Post()
{
Name = "Hello World",
Url = "https://www.experts-exchange.com/questions/29135911/C-Merging-XML-files.html",
Id = 45678,
Number = 7,
ImageCount = 99,
DownloadedImagesCount = 99,
Finished = "true"
};
history.Posts.Add(post);
histories.Add(history);
History result = new History();
foreach (var history in histories.Distinct())
{
result.Posts.AddRange(history.Posts);
}
History.Save(RootDir + "\\history_out.xml", result);
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 2: cannot convert from 'System.Collections.Generic.List<MergeXML.History>' to 'MergeXML.History' MergeXML D:\Vb\Test\MergeXML\MergeXML\Program.cs 42 Active
ASKER
History result = new History();
foreach (var history in histories.Distinct())
{
result.Posts.AddRange(history.Posts);
}
Converts all posts into 1 unique History posts this in turn allows manipulation of the single list eg (Saving, adding a single post and Querying ect)
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
namespace MergeXML
{
class Program
{
static List<History> histories = new List<History>();
static void Main(string[] args)
{
var files = Directory.EnumerateFiles(".", "*history.xml", SearchOption.AllDirectories);
foreach (string file in files)
{
histories.Add(History.Load(file));
}
// Create a singular out history
History result = new History();
foreach (var history in histories)
{
result.Posts.AddRange(history.Posts.Except(result.Posts, new PostComparer()));
}
History.Save(@"history_out.xml", result);
}
}
[XmlRoot(ElementName = "history", DataType = "string", IsNullable = true)]
public class History
{
[XmlElement(ElementName = "post")]
public List<Post> Posts { get; set; } = new List<Post>();
public static History Load(string file)
{
var deserializer = new XmlSerializer(typeof(History));
using (TextReader reader = new StreamReader(file))
{
return deserializer.Deserialize(reader) as History;
}
}
internal static void Save(string file, History source)
{
var serializer = new XmlSerializer(typeof(History));
using (TextWriter writer = new StreamWriter(file))
{
serializer.Serialize(writer, source);
}
}
}
public class Post
{
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "url")]
public string Url { get; set; }
[XmlElement(ElementName = "id")]
public int Id { get; set; }
[XmlElement(ElementName = "number")]
public int Number { get; set; }
[XmlElement(ElementName = "imageCount")]
public int ImageCount { get; set; }
[XmlElement(ElementName = "downloadedImagesCount")]
public int DownloadedImagesCount { get; set; }
[XmlElement(ElementName = "finished")]
public string Finished { get; set; }
}
class PostComparer : IEqualityComparer<Post>
{
public bool Equals(Post x, Post y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
{
return false;
}
return Equals(x.DownloadedImagesCount, y.DownloadedImagesCount) &&
Equals(x.Finished, y.Finished) &&
Equals(x.Id, y.Id) &&
Equals(x.ImageCount, y.ImageCount) &&
Equals(x.Name, y.Name) &&
Equals(x.Number, y.Number) &&
Equals(x.Url, y.Url);
}
public int GetHashCode(Post obj)
{
if (ReferenceEquals(obj, null))
{
return 0;
}
return obj.DownloadedImagesCount.GetHashCode() ^ obj.Finished.GetHashCode() ^
obj.Id.GetHashCode() ^ obj.ImageCount.GetHashCode() ^ obj.Name.GetHashCode() ^
obj.Number.GetHashCode() ^ obj.Url.GetHashCode();
}
}
}
With the same files above and one additional one:<history>
<post>
<name>My Collection 4</name>
<url>https://example.com/hread.php?p=54321</url>
<id>54321</id>
<number>4</number>
<imageCount>135</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
<post>
<name>My Collection 5</name>
<url>https://example.com/hread.php?p=23456</url>
<id>23456</id>
<number>5</number>
<imageCount>136</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
</history>
Produces the following output -
<?xml version="1.0" encoding="utf-8"?>
<history xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<post>
<name>My Collection </name>
<url>https://example.com/hread.php?p=12345</url>
<id>12345</id>
<number>1</number>
<imageCount>132</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
<post>
<name>My Collection 2</name>
<url>https://example.com/hread.php?p=67890</url>
<id>67890</id>
<number>2</number>
<imageCount>133</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
<post>
<name>My Collection 3</name>
<url>https://example.com/hread.php?p=9876</url>
<id>9876</id>
<number>3</number>
<imageCount>134</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
<post>
<name>My Collection 4</name>
<url>https://example.com/hread.php?p=54321</url>
<id>54321</id>
<number>4</number>
<imageCount>135</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
<post>
<name>My Collection 5</name>
<url>https://example.com/hread.php?p=23456</url>
<id>23456</id>
<number>5</number>
<imageCount>136</imageCount>
<downloadedImagesCount>0</downloadedImagesCount>
<finished>true</finished>
</post>
</history>
-saige-
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
Open in new window
-saige-