C
C#14mo ago
Curious

❔ XML Read XML and save as dictionary

Hi all. I want to read XML data and convert to a dictionary, I have given this go with: This is how i write:
......Above
XmlTextWriter writer = new XmlTextWriter(StoredDataFile, System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement(startingText);
//write dict to xml
foreach (KeyValuePair<string, bool> kvp in toWrite)
{
writer.WriteStartElement("Values");
writer.WriteAttributeString("key", kvp.Key);
writer.WriteAttributeString("value", kvp.Value.ToString());
writer.WriteEndElement();
}
......Below
......Above
XmlTextWriter writer = new XmlTextWriter(StoredDataFile, System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement(startingText);
//write dict to xml
foreach (KeyValuePair<string, bool> kvp in toWrite)
{
writer.WriteStartElement("Values");
writer.WriteAttributeString("key", kvp.Key);
writer.WriteAttributeString("value", kvp.Value.ToString());
writer.WriteEndElement();
}
......Below
This is how I am reading:
public static Dictionary<string, bool> ReadFromXML(string tagToRead)
{
Dictionary<string, bool> outDict = new Dictionary<string, bool>();
//look for the tag tagtoread in the XML tree, then return all values in that tag as a dictionary
try
{
XDocument doc = XDocument.Load(StoredDataFile);
XElement root = doc.Root;
var values = doc.Root.Elements(tagToRead).Select(n => n.Value);

//iterate through and get name and value to add to dictionary
foreach (var value in values)
{
string[] split = value.Split(',');
outDict.Add(split[0], Convert.ToBoolean(split[1]));
}
return outDict;

}
catch (Exception ex)
{
Autodesk.Revit.UI.TaskDialog.Show("Read XML Error", ex.Message);
return null;
}

}
public static Dictionary<string, bool> ReadFromXML(string tagToRead)
{
Dictionary<string, bool> outDict = new Dictionary<string, bool>();
//look for the tag tagtoread in the XML tree, then return all values in that tag as a dictionary
try
{
XDocument doc = XDocument.Load(StoredDataFile);
XElement root = doc.Root;
var values = doc.Root.Elements(tagToRead).Select(n => n.Value);

//iterate through and get name and value to add to dictionary
foreach (var value in values)
{
string[] split = value.Split(',');
outDict.Add(split[0], Convert.ToBoolean(split[1]));
}
return outDict;

}
catch (Exception ex)
{
Autodesk.Revit.UI.TaskDialog.Show("Read XML Error", ex.Message);
return null;
}

}
I get an error Pref is an unexpected token. The expected token is = Line 2, position 20.
14 Replies
Curious
Curious14mo ago
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Ribbon Visibility Preferences>
<Values key="Architecture" value="True" />
<Values key="Structure" value="False" />
<Values key="Steel" value="False" />
<Values key="thePrecastTab" value="False" />
</Ribbon Visibility Preferences>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Ribbon Visibility Preferences>
<Values key="Architecture" value="True" />
<Values key="Structure" value="False" />
<Values key="Steel" value="False" />
<Values key="thePrecastTab" value="False" />
</Ribbon Visibility Preferences>
This is an example XML doc
cap5lut
cap5lut14mo ago
well, its invalid xml, names (no matter if attribute or element) are not allowed to contain whitespaces so u would have to rename the root element
Curious
Curious14mo ago
Ribbon Visibility Preferences to Ribbon_Visibility_Preferences? That simple? Values the same then, i.e Archi Tecture would be invalid?
cap5lut
cap5lut14mo ago
values can contain whitespaces, they are wrapped by the double quotes after all ;p but yeah basically its that simple names can contain letters, digits, hyphens, underscores, and periods
Curious
Curious14mo ago
Awesome - Thank you will test it now 😄
0day
0day14mo ago
@Curious r u there
Curious
Curious14mo ago
Yes Hi
0day
0day14mo ago
is it fixed
Curious
Curious14mo ago
No still produces error Pref is an unexpected token. The expected token is = Line 2, position 20. and returns null
Curious
Curious14mo ago
and
Curious
Curious14mo ago
public static Dictionary<string, bool> ReadFromXML(string tagToRead)
{
Dictionary<string, bool> outDict = new Dictionary<string, bool>();
//look for the tag tagtoread in the XML tree, then return all values in that tag as a dictionary
try
{
XmlDocument xml = new XmlDocument();
xml.Load(StoredDataFile);
xml.LoadXml(tagToRead);

XmlNodeList xnList = xml.SelectNodes(tagToRead);
foreach (XmlNode xn in xnList)
{
//get value and get bool and convert to dict
string key = xn.Attributes["key"].Value;
bool value = Convert.ToBoolean(xn.Attributes["value"].Value);

outDict.Add(key, value);
}
return outDict;
}
catch (Exception ex)
{
Autodesk.Revit.UI.TaskDialog.Show("Read XML Error", ex.Message);
return null;
}

}
public static Dictionary<string, bool> ReadFromXML(string tagToRead)
{
Dictionary<string, bool> outDict = new Dictionary<string, bool>();
//look for the tag tagtoread in the XML tree, then return all values in that tag as a dictionary
try
{
XmlDocument xml = new XmlDocument();
xml.Load(StoredDataFile);
xml.LoadXml(tagToRead);

XmlNodeList xnList = xml.SelectNodes(tagToRead);
foreach (XmlNode xn in xnList)
{
//get value and get bool and convert to dict
string key = xn.Attributes["key"].Value;
bool value = Convert.ToBoolean(xn.Attributes["value"].Value);

outDict.Add(key, value);
}
return outDict;
}
catch (Exception ex)
{
Autodesk.Revit.UI.TaskDialog.Show("Read XML Error", ex.Message);
return null;
}

}
private const string StoredDataFile = "C:\\ProgramData\\SDS_TOOLS\\RibbonPreferences.xml"; private const string RibbonVisPref = "Ribbon_Visibility_Preferences";
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Ribbon_Visibility_Preferences>
<Values key="Architecture" value="True" />
<Values key="Structure" value="False" />
<Values key="Steel" value="False" />
<Values key="thePrecastTab" value="False" />
<Values key="Systems" value="True" />
<Values key="Insert" value="True" />
<Values key="Annotate" value="True" />
<Values key="Analyze" value="True" />
<Values key="MassingSite" value="False" />
<Values key="Collaborate" value="False" />
<Values key="View" value="True" />
<Values key="Manage" value="True" />
<Values key="Home_Family" value="False" />
<Values key="Insert_AnnotationDetailModelMassConceptualProfileTrussFamily" value="False" />
<Values key="Annotate_ModelMassFamily" value="False" />
<Values key="View_Family" value="False" />
<Values key="Manage_Family" value="False" />
<Values key="Add-Ins" value="False" />
<Values key="Modify" value="True" />
<Values key="Second_Modify" value="False" />
<Values key="InPlaceModelFamilyTab" value="False" />
<Values key="InPlaceMassFamilyTab" value="False" />
<Values key="InPlaceZoneFamilyTab" value="False" />
<Values key="FamilyEditorTab" value="False" />
</Ribbon_Visibility_Preferences>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Ribbon_Visibility_Preferences>
<Values key="Architecture" value="True" />
<Values key="Structure" value="False" />
<Values key="Steel" value="False" />
<Values key="thePrecastTab" value="False" />
<Values key="Systems" value="True" />
<Values key="Insert" value="True" />
<Values key="Annotate" value="True" />
<Values key="Analyze" value="True" />
<Values key="MassingSite" value="False" />
<Values key="Collaborate" value="False" />
<Values key="View" value="True" />
<Values key="Manage" value="True" />
<Values key="Home_Family" value="False" />
<Values key="Insert_AnnotationDetailModelMassConceptualProfileTrussFamily" value="False" />
<Values key="Annotate_ModelMassFamily" value="False" />
<Values key="View_Family" value="False" />
<Values key="Manage_Family" value="False" />
<Values key="Add-Ins" value="False" />
<Values key="Modify" value="True" />
<Values key="Second_Modify" value="False" />
<Values key="InPlaceModelFamilyTab" value="False" />
<Values key="InPlaceMassFamilyTab" value="False" />
<Values key="InPlaceZoneFamilyTab" value="False" />
<Values key="FamilyEditorTab" value="False" />
</Ribbon_Visibility_Preferences>
cap5lut
cap5lut14mo ago
the error is in there:
xml.Load(StoredDataFile); // reads the file and parses its contents
xml.LoadXml(tagToRead); // treats the passed string as xml and parses it
xml.Load(StoredDataFile); // reads the file and parses its contents
xml.LoadXml(tagToRead); // treats the passed string as xml and parses it
basically u need to get rid of the xml.LoadXml(tagToRead); line
Curious
Curious14mo ago
Ohhh makes sense to be fair thank you
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.