我想讀取XML文件並保存InnerText字符串,但它與SystemNullReference錯誤崩潰。我認爲我閱讀的方式是錯誤的,但我不確定。C#/ XML讀取XML文件不起作用
XML的文件:
<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
<schema>
<Hintergrund>#FFFFFFFF</Hintergrund>
<Zahlen>#FFFFFFFF</Zahlen>
<Text>#FFFFFFFF</Text>
<Zeichenketten>#FFFFFFFF</Zeichenketten>
<Tags>#FF000000</Tags>
<Direktiven>#FF000000</Direktiven>
<Ausdruecke>#FF000000</Ausdruecke>
<Initialisierer>#FF000000</Initialisierer>
<Modifizierer>#FF000000</Modifizierer>
<Booleanische>#FF000000</Booleanische>
<Operator>#FF000000</Operator>
<Kommentare>#FF000000</Kommentare>
</schema>
閱讀:
foreach (XmlNode node in doc.SelectNodes("/schema/*"))
{
switch (node.Name)
{
case "Hintergrund":
colorBG = (Color)ColorConverter.ConvertFromString(node["Hintergrund"].InnerText);
break;
case "Zahlen":
colorNumbers = (Color)ColorConverter.ConvertFromString(node["Zahlen"].InnerText);
break;
case "Text":
colorText = (Color)ColorConverter.ConvertFromString(node["Text"].InnerText);
break;
case "Zeichenketten":
colorStrings = (Color)ColorConverter.ConvertFromString(node["Zeichenketten"].InnerText);
break;
case "Tags":
colorTags = (Color)ColorConverter.ConvertFromString(node["Tags"].InnerText);
break;
case "Direktiven":
colorDirectives = (Color)ColorConverter.ConvertFromString(node["Direktiven"].InnerText);
break;
case "Ausdruecke":
colorStatements = (Color)ColorConverter.ConvertFromString(node["Ausdruecke"].InnerText);
break;
case "Initialisierer":
colorInitial = (Color)ColorConverter.ConvertFromString(node["Initialisierer"].InnerText);
break;
case "Modifizierer":
colorModifiers = (Color)ColorConverter.ConvertFromString(node["Modifizierer"].InnerText);
break;
case "Booleanische":
colorBools = (Color)ColorConverter.ConvertFromString(node["Booleanische"].InnerText);
break;
case "Operator":
colorOperators = (Color)ColorConverter.ConvertFromString(node["Operator"].InnerText);
break;
case "Kommentare":
colorComments = (Color)ColorConverter.ConvertFromString(node["Kommentare"].InnerText);
break;
}
}
請告訴我,爲什麼它不工作。
那麼哪條線路故障?你已經嘗試了哪些診斷? –
是否需要switch語句?所有這些元素都包含顏色。 – Gusdor