C
C#2y 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
CuriousOP2y 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
cap5lut2y 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
CuriousOP2y ago
Ribbon Visibility Preferences to Ribbon_Visibility_Preferences? That simple? Values the same then, i.e Archi Tecture would be invalid?
cap5lut
cap5lut2y 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
CuriousOP2y ago
Awesome - Thank you will test it now 😄
0day
0day2y ago
@Curious r u there
Curious
CuriousOP2y ago
Yes Hi
0day
0day2y ago
is it fixed
Curious
CuriousOP2y ago
No still produces error Pref is an unexpected token. The expected token is = Line 2, position 20. and returns null
Curious
CuriousOP2y ago
and
Curious
CuriousOP2y 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
cap5lut2y 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
CuriousOP2y ago
Ohhh makes sense to be fair thank you
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server