如何将下面的xlm文件转换为excel,其中节点作为列名称,其值作为c#中的行

时间:2016-04-10 10:34:01

标签: c# xml excel

<ENVELOPE>
    <HEADER>
        <TALLYREQUEST>Import Data</TALLYREQUEST>
    </HEADER>
    <BODY>
        <IMPORTDATA>
            <REQUESTDESC><REPORTNAME>All Masters</REPORTNAME><STATICVARIABLES><SVCURRENTCOMPANY>MSIT</SVCURRENTCOMPANY></STATICVARIABLES></REQUESTDESC>
            <REQUESTDATA>
                <TALLYMESSAGE>
    <Entry_x0020_NO.>125</Entry_x0020_NO.>
    <DATE>12</DATE>
    <GUID>1258</GUID>
    <NARRATION>1542</NARRATION>
    <VOTURETYPENAME>456</VOTURETYPENAME>
    <NAME>achuth</NAME>
    <AMOUNT>250</AMOUNT>
  </TALLYMESSAGE>
            </REQUESTDATA>
        </IMPORTDATA>
    </BODY>
</ENVELOPE>

将上述XML作为输入并转换为excel,如下所示

Entry No.   date  guid   narration   voucher   name   amount  
125         12     1258     1542      456      achuth  250

我的代码抛出错误空引用,如下面enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将string实例转换为Value而不是访问其...... new YourClass() { YourClassProperty = (string)s.Element("Your_element_that_may_not_exists"), ...... } ...... 属性,以避免在父元素中找不到目标元素的情况下出现此类异常:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.abc.xyz.AboutActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:background="@color/toolbarTransparent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/header_about"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="@color/colorAccent"/>

    <ImageView
        android:id="@+id/a"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:src="@drawable/a"
        app:layout_anchor="@id/header_about"
        app:layout_anchorGravity="bottom|center"/>

    <include layout="@layout/content_about"/>

</android.support.design.widget.CoordinatorLayout>