用c#读取xml文件

时间:2013-04-02 11:25:01

标签: c# .net xml linq-to-xml

我有一个奇怪的xml文件,我想阅读c#,我在互联网上搜索了很多,但对我的案例没有太多了解

<XMLFile>
<Template>
<TestElement ID="22">
    <PlugIn Name="ESA620" TestID="1240"/>
    <Info>
        <Type>Current 1</Type>
        <Parameter>Current 1</Parameter>
        <Name>Current 1</Name>
    </Info>
    <ExpectedResult>
        <Standard AlphaName="IEC" CompleteName="IEC 60601" Type="Predefined">
            <Limit Key="::ST">
                <Unit>uA</Unit>
                <Value Type="High">100</Value>
            </Limit>
        </Standard>
    </ExpectedResult>
</TestElement>
.   .   .   .   .   .
<TestElement ID="35">
    <PlugIn Name="ESA620" TestID="1500"/>
    <Info>
        <Type>Current auxiliary</Type>
        <Name>Current auxiliary</Name>
    </Info>
    <ExpectedResult>
        <Standard AlphaName="IEC" CompleteName="IEC 60601">
            <Limit Key="::B">
                <Unit>uA</Unit>
            </Limit>
            <Limit Key="::BF">
                <Unit>uA</Unit>
            </Limit>
            <Limit Key="::CF">
                <Unit>uA</Unit>
            </Limit>
            <Limit Key="::B_DC">
                <Unit>uA</Unit>
            </Limit>
            <Limit Key="::BF_DC">
                <Unit>uA</Unit>
            </Limit>
            <Limit Key="::CF_DC">
                <Unit>uA</Unit>
            </Limit>
        </Standard>
    </ExpectedResult>
    <PlugInData PlugIn="ESA620">
        <ValueFilter>None</ValueFilter>
        <EnableBtn Skip="True" NA="True" Step="True"/>
        <ModuleOptions Sep="False" Exp="False" Stop="False"/>
        <TestOptions>
            <Run Opt="False" HaltOnFail="True" MultiPE="False" TotalLeakage="False" MeasType="0" TestVoltage="0" AppliedParts="0"/>
            <StopOnPC Before="False" After="False"/>
            <Delay On="2" Off="0"/>
        </TestOptions>
    </PlugInData>
    <TestElement ID="36">
        <PlugIn Name="ESA620" TestID="1510"/>
            <Info>
                <Type>Patient Current auxiliary </Type>
                <Parameter>Normal Condition</Parameter>
                <Name>Normal Condition</Name>
            </Info>
            <ExpectedResult>
            <Standard AlphaName="IEC" CompleteName="IEC 60601" Type="Predefined">
                <Limit Key="::B">
                    <Unit>uA</Unit>
                    <Value Type="High">100</Value>
                </Limit>
                <Limit Key="::BF">
                    <Unit>uA</Unit>
                    <Value Type="High">100</Value>
                </Limit>
                <Limit Key="::CF">
                    <Unit>uA</Unit>
                    <Value Type="High">10</Value>
                </Limit>
                <Limit Key="::B_DC">
                    <Unit>uA</Unit>
                    <Value Type="High">10</Value>
                </Limit>
                <Limit Key="::BF_DC">
                    <Unit>uA</Unit>
                    <Value Type="High">10</Value>
                </Limit>
                <Limit Key="::CF_DC">
                    <Unit>uA</Unit>
                    <Value Type="High">10</Value>
                </Limit>
            </Standard>
        </ExpectedResult>
    </TestElement>
    <TestElement ID="37">
        <PlugIn Name="ESA620" TestID="1520"/>
        <Info>
            <Type>Patient Current auxiliary</Type>
            <Parameter>Neutral open</Parameter>
            <Name>Neutral open</Name>
        </Info>
        <ExpectedResult>
            <Standard AlphaName="IEC" CompleteName="IEC 60601" Type="Predefined">
                <Limit Key="::B">
                    <Unit>uA</Unit>
                    <Value Type="High">500</Value>
                </Limit>
                <Limit Key="::BF">
                    <Unit>uA</Unit>
                    <Value Type="High">500</Value>
                </Limit>
                <Limit Key="::CF">
                    <Unit>uA</Unit>
                    <Value Type="High">50</Value>
                </Limit>
                <Limit Key="::B_DC">
                    <Unit>uA</Unit>
                    <Value Type="High">50</Value>
                </Limit>
                <Limit Key="::BF_DC">
                    <Unit>uA</Unit>
                    <Value Type="High">50</Value>
                </Limit>
                <Limit Key="::CF_DC">
                    <Unit>uA</Unit>
                    <Value Type="High">50</Value>
                </Limit>
            </Standard>
        </ExpectedResult>
    </TestElement>
    <TestElement ID="38">
.   .   .   .   .
    </TestElement>
</TestElement>
</Template>
<Result>
<ResultItem MTI="1" ElementID="22">
    <Status>Passed</Status>
    <Measurement Key="::ST">
        <Description>normal conditions, reverse polarity</Description>
        <Value Type="Float">.2</Value>
        <Unit>uA</Unit>
        <Status>Passed</Status>
    </Measurement>
</ResultItem>
.   .   .   .   .   
<ResultItem ElementID="35">
    <Status>Passed</Status>
    <ResultItem MTI="1" ElementID="36">
        <Status>Passed</Status>
        <Measurement Key="::1::B">
            <Description>patient circuit</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
        <Measurement Key="::2::CF">
            <Description>ECG</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
        <Measurement Key="::3::B">
            <Description>Spo2</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
        <Measurement Key="::4::B">
            <Description>NiBp</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
    </ResultItem>
    <ResultItem MTI="1" ElementID="37">
        <Status>Passed</Status>
        <Measurement Key="::1::B">
            <Description>Patient circuit</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
        <Measurement Key="::2::CF">
            <Description>ECG</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
        <Measurement Key="::3::B">
            <Description>Spo2</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
        <Measurement Key="::4::B">
            <Description>NiBp</Description>
            <Value Type="Float">.1</Value>
            <Unit>uA</Unit>
            <Status>Passed</Status>
        </Measurement>
    </ResultItem>
<ResultItem MTI="1" ElementID="38">
.   .   .   .   .   .
</ResultItem>
</ResultItem>
</Result>
</XMLFile>

我的xml文件由两部分组成,第一部分是模板,第二部分是值。 我试图得到的例子:

  

当前1:当前1,单位:uA,极限高:100,值:.2,状态   通过

     

当前辅助:电流辅助:患者电流辅助:   患者回路:单位:uA,极限高:100,值:.1状态:已通过

     

心电图:单位:uA,极限高:10,值:.1状态:已通过

     

Spo2:单位:uA,限制高:100,值:.1状态:通过

     

NiBp:单位:uA,限制高:100,值:.1状态:通过

     

中性开放:患者电流辅助:患者电路:单位:uA,   限制高:500,值:.1状态:通过

     

心电图:单位:uA,极限高:50,值:.1状态:已通过

     

Spo2:单位:uA,限制高:500,值:.1状态:通过

     

NiBp:单位:uA,限制高:500,值:.1状态:通过

如何用c#读取这个xml?

1 个答案:

答案 0 :(得分:1)

XDocument.Load

XDocument.Parse

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
    <Child>Content</Child>
</Root>";
XDocument doc = XDocument.Parse(str);
Console.WriteLine(doc);