Thursday, 11 December 2014

Get all atributes of an XML to an 2d or 3d string array in C#



For my project I'm trying to translate or decrypt a XML API answer to an usable array of all the information.


Here is an example of the XML I'm receiving:



<users>
<User LoginName="test1" Owner="" Alias="" UserType="PAID" ClientType="OBM" Quota="10737418240" Timezone="GMT+08:00 (CST)" Language="en" DataFile="1" DataSize="1536" RetainFile="0" RetainSize="0" EnableMSSQL="Y" EnableMSExchange="Y" EnableOracle="Y" EnableLotusNotes="Y" EnableLotusDomino="Y" EnableMySQL="Y" EnableInFileDelta="Y" EnableShadowCopy="Y" EnableExchangeMailbox="N" ExchangeMailboxQuota="0" EnableNASClient="Y" EnableDeltaMerge="Y" EnableMsVm="N" MsVmQuota="0" EnableVMware="N" VMwareQuota="0" Bandwidth="0" Notes="" Status="ENABLE" RegistrationDate="1302687743242" SuspendPaidUser="N" SuspendPaidUserDate="20140503" LastBackupDate="1302699594652" EnableCDP="Y" EnableShadowProtectBareMetal="Y"EnableWinServer2008BareMetal="Y"
Hostname="123.abc.com" FileSizeLimit="52428800" ExcludeNetworkShare="Y"
><Contact Name=""Email="www@qqq.com"/>
</user>


I've succeeded to get one Attribute at once by using this code below:



/// <summary>
/// Get attribute from XML file USE ONLY AS DEMO
/// </summary>
/// <param name="inputXML">XML string</param>
/// <param name="requestVar">The requested variable in the XML response</param>
/// <param name="parameter">Where the requested variable is located</param>
/// <returns>string of all requested variables</returns>
public static String GetAttribute(string inputXML, string requestVar, string parameter)
{
string vars = "";
XmlDocument xml = new XmlDocument();
xml.LoadXml(inputXML);

XmlNodeList xnList = xml.GetElementsByTagName(parameter);
foreach (XmlNode xn in xnList)
{
vars = vars + xn.Attributes[requestVar].Value;
}
return vars;
}


This function needs the node names where it can be found. For this project are many API calls necessary and would like a function which puts all the attributes in a string array. So far I tried to translate this part:



vars = vars + xn.Attributes[requestVar].Value;


to this:



foreach (XmlAttribute xa in xn)
{
vars[i, j, k] = xa.Value;
k++;
}


and I Also tried:



for (k = 0; k < xn.Attributes.Count; k++ )
{
vars[i, j, k] = xn.Attributes[k].Value;
}


But both codes won't work. How do I get a simpel for loop or foreach, which gets all the attributes in the array? And can this also be done with:



XmlNodeList xnList = xml.GetElementsByTagName(parameter);


The i, j and k vars are used in the multiple loops: i is used for XmlNodeList, the j is used for the XmlNode and the k is used for the XmlAttribute.


In this array I would like to get all the info of the XML file in the same order, only the parts between the '""' is needed.



vars[0][0][0]= would stand for: <Users><User LoginName= (vars[<Users>][<User][LoginName]=


This is the function as far as I'm now:



public static String[,,] GetVars(string inputXML)
{
string[,,] vars = new string[100,50,50];
int i, j, k;
XmlDocument xml = new XmlDocument();
xml.LoadXml(inputXML);

i = j = k = 0;
XmlNodeList xnList = xml.GetElementsByTagName("Users");
foreach (XmlNode xn in xnList)
{
foreach (XmlAttribute xa in xn)
{
vars[i, j, k] = xa.Value;
k++;
}
k = 0;
j++;
}
j = 0;
return vars;

No comments:

Post a Comment