阅读双嵌套xml

时间:2012-10-05 19:47:20

标签: c# xml nested

我试图阅读以下XML文件

<Tabellen>
<Header>
    <Tables>
        <Table>SW_RL</Table>
            <Fields>
                <Field>KeyVeld</Field>
                <Type>Alpha</Type>
                <Size>100</Size>
                <SubType>st_none</SubType>
            </Fields>
            <Fields>
                <Field>ErrorCode</Field>
                <Type>Alpha</Type>
                <Size>50</Size>
                <SubType>st_none</SubType>
            </Fields>
            <Fields>
                <Field>Description</Field>
                <Type>Memo</Type>
                <Size>100</Size>
                <SubType>st_none</SubType>
            </Fields>
        </Tables>
</Header>
<Header>
    <Tables>
        <Table>SW_RH</Table>
    <Fields>
        <Field>DocNum</Field>
        <Type>Numeric</Type>
        <Size>0</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>PaymentTerms</Field>
        <Type>Numeric</Type>
        <Size>0</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>DocDate</Field>
        <Type>Date</Type>
        <Size>0</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>Contract_Start</Field>
        <Type>Date</Type>
        <Size>0</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>Contract_End</Field>
        <Type>Date</Type>
        <Size>0</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>CardCode</Field>
        <Type>Alpha</Type>
        <Size>40</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>CardName</Field>
        <Type>Alpha</Type>
        <Size>100</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>BillToAddress</Field>
        <Type>Alpha</Type>
        <Size>100</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>ShipToAddress</Field>
        <Type>Alpha</Type>
        <Size>100</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>NumAtCard</Field>
        <Type>Alpha</Type>
        <Size>100</Size>
        <SubType>st_none</SubType>
    </Fields>
    <Fields>
        <Field>BillingPeriod</Field>
        <Type>Alpha</Type>
        <Size>10</Size>
        <SubType>st_none</SubType>      
    </Fields>
    <Fields>
        <Field>AdvanceBilling</Field>
        <Type>Alpha</Type>
        <Size>10</Size>
        <SubType>st_none</SubType>
    </Fields>
    </Tables>
</Header>
</Tabellen>

当前代码

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(directory + "\\Server_Config.xml");
XmlNodeList xmlNodelist = null;
XmlNodeList xmlNodelist2 = null;

xmlNodelist = xmldoc.SelectNodes("Tabellen/Header/Tables");
foreach (XmlNode mnode in xmlNodelist)
{
    MessageBox.Show(mnode["Table"].InnerText, "ok");
     //create table

    xmlNodelist2 = xmldoc.SelectNodes("Tabellen/Header/Tables/Fields");
    foreach (XmlNode mnode2 in xmlNodelist2)
    {
        MessageBox.Show(mnode2["Field"].InnerText, "ok");
        // create fields
    }

}

一般的想法是创建2个表 表1:包含3个字段的RL 表2:包含12个字段的RH

但实际上结果是两个表都包含15个字段

我做错了什么?

2 个答案:

答案 0 :(得分:1)

Linq2Xml更易于使用。

XDocument xDoc = XDocument.Load("myfile.xml");
var tables = xDoc.Descendants("Header")
                .Select(h => new
                {
                    Table  = h.Descendants("Table").First().Value,
                    Fields = h.Descendants("Fields")
                              .Select(f=>new{
                                    Field = f.Element("Field").Value,
                                    Type = f.Element("Type").Value,
                                    Size = f.Element("Size").Value,
                                    SubType = f.Element("SubType").Value
                               })
                              .ToList()
                })
                .ToList();

答案 1 :(得分:0)

xmlNodelist2 = xmldoc.SelectNodes("Tabellen/Header/Tables/Fields");

应该是

xmlNodelist2 = mnode.SelectNodes("Fields");

否则,您选择XML中的所有字段,而不仅仅是当前表下的字段。

(但我没有测试过代码)