I have thousands of XML files, each one describing the properties of an object. Each object being different which means the XML files are not the same. I am trying to write a function for each bit of data I would like to parse out of the files.
The purpose of this is to gather the information. Create a new object and then write it to disk as an encrypted file. I have looked into serialization but I think because of inconsistencies in the data provided to me that It could cause me issues.
Here is an example of the XML :
<TestXMl Var1="000" Var2="000" Var3="01">
<var4>testdata</var4>
<var5>testdata</var5>
<var6>testdata</var6>
<DeeperLevel>
<var7>testdata</var7>
<var8>testdata</var8>
<var9>testdata</var9>
<var10>testdata</var10>
</Deeper
Level>
</TestXMl>
I have written a function which is currently working for Var4 through Var6. I am unable to get Var1, Var2 and Var3 at this stage.
private string Var1;
private string Var2;
private string Var3;
private string Var4;
private string Var5;
private string Var6;
//Var4
public void ParseVar4(string Directory)
{
XmlDocument doc = new XmlDocument();
doc.Load(Directory);
// Retrieve all开发者_StackOverflow prices.
XmlNodeList nodeList = doc.GetElementsByTagName("var4");
foreach (XmlNode node in nodeList)
{
Var4= node.ChildNodes[0].Value;
}
}
public string GetVar4()
{
return Var4;
}
I use the above function for both var5 and var6. However I am unable to get this function to work with the other vars. I am a little unsure If I am going in the write direction with the function. All advice is very welcome.
Cheers in advance.
Instead of individual string properties, you could use a List<string>
and gather an arbitrary number of them. Then, you can reference them by index.
Consider using XPaths. They are the query language for XML like SQL is to RDBMS.
Here's a sample
static void Main(string[] args)
{
string s = "<TestXMl Var1=\"000\" Var2=\"000\" Var3=\"01\"><var4>testdata</var4><var5>testdata</var5><var6>testdata</var6><DeeperLevel><var7>testdata</var7><var8>testdata</var8><var9>testdata</var9><var10>testdata</var10></DeeperLevel></TestXMl> ";
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(s);
string var1 = xDoc.SelectSingleNode("/TestXMl/attribute::Var1").Value;
Console.WriteLine(var1);
string var2 = xDoc.SelectSingleNode("/TestXMl/attribute::Var2").Value;
Console.WriteLine(var2);
string var3 = xDoc.SelectSingleNode("/TestXMl/attribute::Var3").Value;
Console.WriteLine(var3);
string var4 = xDoc.SelectSingleNode("/TestXMl/var4").InnerText;
Console.WriteLine(var4);
string var5 = xDoc.SelectSingleNode("/TestXMl/var5").InnerText;
Console.WriteLine(var5);
string var6 = xDoc.SelectSingleNode("/TestXMl/var6").InnerText;
Console.WriteLine(var6);
string var7 = xDoc.SelectSingleNode("/TestXMl/DeeperLevel/var7").InnerText;
Console.WriteLine(var7);
string var8 = xDoc.SelectSingleNode("/TestXMl/DeeperLevel/var8").InnerText;
Console.WriteLine(var8);
string var9 = xDoc.SelectSingleNode("/TestXMl/DeeperLevel/var9").InnerText;
Console.WriteLine(var9);
string var10 = xDoc.SelectSingleNode("/TestXMl/DeeperLevel/var10").InnerText;
Console.WriteLine(var10);
}
Hope this helps ... ;)
var1,2 and 3 are attributes not element.
Try
XmlAttributeCollection atribs = doc.DocumentElement.Attributes;
精彩评论