我得到了这种 xml 文档:
<root>
<class_table>
<class_title>
<class_name>SomeClassBla</class_name>
</class_title>
<fields>
<field_name>DateTime _date</field_name>
<field_name>string _posterName</field_name>
<field_name>string _commentText</field_name>
<field_name>bool _visible</field_name>
</fields>
<properties>
<property_name>DateTime Date</property_name>
<property_name>string PosterName</property_name>
<property_name>string CommentText</property_name>
<property_name>bool Visible</property_name>
</properties>
<methods />
<inheritance />
</class_table>
<class_table>
<class_title>
<class_name>someAnotnerClass</class_name>
</class_title>
<fields>
<field_name>int result</field_name>
<field_name>string test</field_name>
</fields>
<properties>
<property_name>string BlogPage</property_name>
<property_name>string BlogPostPage</property_name>
<property_name>string ErrorPage</property_name>
<property_name>string ComingSoonPage</property_name>
</properties>
<methods>
<method_name>string DateFormatter()</method_name>
<method_name>string EncodeBase64()</method_name>
<method_name>string DecodeBase64()</method_name>
<method_name>string CategoriesFormatterTyped()</method_name>
<method_name>string AddShareThisLink()</method_name>
<method_name>string ShortenText()</method_name>
<method_name>string CommentCountFormatter()</method_name>
<method_name>string MtaShorten()</method_name>
</methods>
<inheritance />
</class_table>
</root>
那么我如何读取例如这 2 个 class_table 节点的所有字段名称?它的嵌套深层 im tring 使用此代码:
XmlDocument readDiagramXml = new XmlDocument();
readDiagramXml.Load(classDiagramFile);
XmlNodeList classTables = readDiagramXml.GetElementsByTagName("class_table");
foreach (XmlNode items in classTables)
{
string className = items["fields/fields_name"].InnerText;
File.AppendAllText("A_class_diagram_test.txt", className + Environment.NewLine);
}
我得到对象引用错误。我想要的只是将每个节点的值写入具有该格式的 txt 文件:类名 = SomeClassBla 字段:DateTime _date 字段:字符串 _posterName 等等