I have this code that is supposed to load the attributes of an XML file to a string variable when the user clicks a button:
public void Button1_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load("C:/Structure.xml");
Visit(doc.Root);
}
public static void Visit(XElement element)
{
string siteURL1 = element.Attribute("URL").Value;
string siteTitle1 = element.Attribute("siteTitle").Value;
string siteDescription1 = element.Attribute("siteDescription").Value;
string siteTemplate = element.Attribute("siteTemplate").Value;
string name = element.Attribute("type").Value;
Execute(name, siteURL1, siteTitle1, siteDescription1, siteTemplate);
}
But when I deploy the webpart, and click the button, I get the “NullreferenceException was unhandled by user code / Object reference not set to an instance of an object” errors. on:
string siteURL1 = element.Attribute("URL").Value;"
Any idea of what I may be doing wrong?
This is what the structure looks something like this:
<root>
<level1 name="level1A"
type="Private"
template="3
siteTitle="Private"
siteDescription="Private Site"
URL"private">
<level2 name="level2A">
<level3 name="level3A">
<level4 name="level4A">
<level5 name="level5A">
<level6 name="level6A">
<level7 name="level7A">
<level8 name="level8A"></level8>
</level7>
</level6>
</level5>
</level4>
</level3>
</level2>
</level1>
</root>
One of two things I could see being wrong…
Either the
objectelementisnullor
the
attributeURL doesn’t exist for thatelementTry something like…