create xml file from xml stored in a DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    create xml file from xml stored in a DB

    I have a table in my database that has a 3 fields.

    RuleID | RuleName | Rule

    the ruleID is a randomly generated string of characters, RuleName is the name the user gives to the rule, and the Rule field is about 600 characters long and is just XML text.

    I want to read that Rule field from the database and use it inside the function below.

    Code:
    private static List<MenuItem> LoadRules(bool evaluationType)
    		{
    			//string path = HttpContext.Current.Server.MapPath(string.Format("/Rules/{0}/{1}/", ip, evaluationType ? "Evaluation" : "Execution"));
                List<MenuItem> list = new List<MenuItem>();
    			//if(Directory.Exists(path))
    			{
    				//foreach(string file in Directory.GetFiles(path))
    				{
    					XmlDocument xml = new XmlDocument();
                        
                        xml.Load(file);
    					XmlNamespaceManager m = new XmlNamespaceManager(xml.NameTable);
    					m.AddNamespace("x", xml.DocumentElement.NamespaceURI);
    					XmlNode rule = xml.SelectSingleNode("/x:codeeffects/x:rule", m);
    					list.Add(new MenuItem(
    						rule.Attributes["id"].Value,
    						rule.SelectSingleNode("x:name", m).InnerText,
    						rule.SelectSingleNode("x:description", m) == null ? null : rule.SelectSingleNode("x:description", m).InnerText));
    				}
    			}
    			return list;
    		}
    This function loads an xml file from a static location and parses out some information to a context menu.

    BUt i'm culeless on how to have the function read the xml info found inside my database.

    I'm a rookie at C# so be gentle
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    #2
    Ok, i got part of it figured out but now i'm getting some "object refernce" error.

    Code:
    private static List<MenuItem> LoadRules(bool evaluationType)
    		{
    			string path = HttpContext.Current.Server.MapPath(string.Format("/Rules/{0}/{1}/", ip, evaluationType ? "Evaluation" : "Execution"));
                string xmlstring = LoadRuleXmlFromDB();
                File.WriteAllText(path + "rule.xml", xmlstring);
                
                List<MenuItem> list = new List<MenuItem>();
    			//if(Directory.Exists(path))
    			{
    				foreach(string file in Directory.GetFiles(path))
    				{
                        
    					XmlDocument xml = new XmlDocument();
                        xml.Load(file);
    					XmlNamespaceManager m = new XmlNamespaceManager(xml.NameTable);
    					m.AddNamespace("x", xml.DocumentElement.NamespaceURI);
    					XmlNode rule = xml.SelectSingleNode("/x:codeeffects/x:rule", m);
    					list.Add(new MenuItem(
    						rule.Attributes["id"].Value,
    						rule.SelectSingleNode("x:name", m).InnerText,
    						rule.SelectSingleNode("x:description", m) == null ? null : rule.SelectSingleNode("x:description", m).InnerText));
    				}
    			}
    			return list;
    		}
    As you can see, i created a string xmlstring, loaded my xml from the DB into the string, then wrote the string to a 'rule.xml' file.

    But now, i'm getting "object reference not set to an instance of an object" once the code gets to the
    Code:
    list.Add(new MenuItem(
    						rule.Attributes["id"].Value,
    						rule.SelectSingleNode("x:name", m).InnerText,
    						rule.SelectSingleNode("x:description", m) == null ? null : rule.SelectSingleNode("x:description", m).InnerText));
    part.

    Upon further probing, i have found that the "rule" is null. Not sure why this is happening.

    Comment

    Working...