-1

我得到了这种 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 等等

4

4 回答 4

2

After adding a root element to your xml, you can easily parse it with Linq2Xml

XDocument xDoc = XDocument.Parse(xml); //or .Load(filename)
var classes = xDoc
    .Descendants("class_table")
    .Select(clazz => new
    {
        Name = clazz.Descendants("class_name").First().Value,
        Fields = clazz.Descendants("field_name").Select(x=>x.Value).ToList(),
        Properties = clazz.Descendants("property_name").Select(x=>x.Value).ToList(),
        Methods = clazz.Descendants("method_name").Select(x=>x.Value).ToList(),
    })
    .ToList();

EDIT

And another example using XmlSerializer

//Assuming your(missing) root element is "root"
XmlSerializer ser = new XmlSerializer(typeof(class_table[]),new XmlRootAttribute("root"));
class_table[] obj = (class_table[])ser.Deserialize(stream);


public class class_table
{
    [XmlArrayItem("field_name")]
    public List<string> fields;

    [XmlArrayItem("property_name")]
    public List<string> properties;

    [XmlArrayItem("method_name")]
    public List<string> methods;
}
于 2012-08-28T19:36:13.710 回答
0

you must just add root element to your document, re test your code with this format

    <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>
于 2012-08-28T19:34:29.140 回答
0

有多个项目返回items["field/field_name"],我看不到任何与“field/fields_name”相对应的元素尝试从代码中删除“s”items["fields/fields_name"]

尝试这样的事情:

(我还没有尝试过,所以可能需要进行一些调试,但我认为这是你的问题)

XmlNodeList classTables = xml.SelectNodes("//class_table/fields/fields_name");
//XmlNodeList classTables = readDiagramXml.GetElementsByTagName("class_table");
foreach (XmlNode items in classTables)
{
    string className = items.InnerText;
    foreach (
    File.AppendAllText("A_class_diagram_test.txt", className + Environment.NewLine);
}
于 2012-08-28T19:44:50.347 回答
0

您提供的 XML 似乎没有根元素。所以当你试图解析它时它会抛出一个错误。我会添加一个根元素

<root>
   <class_table>
    //some content
   </class_table>
   <class_table>
    //some content
   </class_table>
</root>

现在我们可以使用 LINQtoXML 来解析它。

var path = Server.MapPath("~/Content/pathToYourFile.xml");           
XElement elm = XElement.Load(path);
if (elm != null)
{
    foreach (var item in elm.Elements("class_table"))
    {
        string className = item.Element("class_title").
                                              Element("class_name").Value;       

        foreach (var field in item.Element("fields").
                                               Elements("field_name"))
        {
            string fieldName = field.Value;
        }
        foreach (var prop in item.Element("properties").
                                                    Elements("property_name"))
        {
            string propName = prop.Value;
        }

    }
}
于 2012-08-28T19:49:47.853 回答