locked
XML to .txt file conversion in C# RRS feed

  • Question

  • Hi Friends,
    I am working in C# projects.
    Currently I have to work with XML files and I need to convert XML file to .txt files.
    I developed one sample application and I used blind method like I hardcoded the node type as below.w.


              while (textReader.Read())
                {
                    XmlNodeType nType = textReader.NodeType;
                    // if node type is an element
                    if (nType == XmlNodeType.Element)
                    {
                        if (textReader.Name.ToString() == "HouseInfo")
                        {
                            strHouseholdID = string.Empty;
                            strHouseHold = iAccountData.ToString() + strcolsep;
                            if (textReader.HasAttributes == true)
                            {
                                for (int index = 0; index < textReader.AttributeCount; index++)
                                {
                                    if (index == 0)
                                        strHouseholdID += textReader.GetAttribute(index).ToString();
                                    if (textReader.GetAttribute(index).ToString() == "")
                                        strHouseHold += "NULL" + "|";
                                    else
                                        strHouseHold += textReader.GetAttribute(index).ToString() + strcolsep;
                                }

                                strHouseHold = strHouseHold.Substring(0, strHouseHold.Length - 1);
                            }
                            swHouseHold.WriteLine(strHouseHold);
                            strHouseHold = string.Empty;
                            iAccountData++;
                        }

                        if (textReader.Name.ToString() == "Package")
                        {
                            strPackage = iAuthPackage.ToString() + strcolsep;
                            if (textReader.HasAttributes == true)
                                for (int index = 0; index < textReader.AttributeCount; index++)
                                {
                                    strPackage += strHouseholdID + strcolsep + textReader.GetAttribute(index).ToString();
                                }

                            swPackage.WriteLine(strPackage);
                            strPackage = string.Empty;
                            iAuthPackage++;
                        }
                       if (textReader.Name.ToString() == "AccountData")
                        {
                            strstbAccount = iStbAccountData.ToString() + strcolsep;
                            if (textReader.HasAttributes == true)

                                strstbAccount += strHouseholdID;
                                for (int index = 0; index < textReader.AttributeCount; index++)
                                {
                                    strstbAccount += strcolsep + textReader.GetAttribute(index).ToString();
                                }

                            //strstbAccount = strstbAccount.Substring(0, strstbAccount.Length - 1);

                            swstbAccount.WriteLine(strstbAccount);
                            strstbAccount = string.Empty;
                            iStbAccountData++;
                        }

      }
         }



    the xml file is

    <ABC>
        <AccountDataList>
          <HouseInfo accountId="1900" zip="55416" dma="613" timeZoneLabel="">

            <AuthPackageList>
              <Package id="AC"/>
              <Package id="BF"/>
            </AuthPackageList>

            <StbAccountDataList>
              <AccountData stbId="987456" clientVersion="1.0" />
              <AccountData stbId="987457" clientVersion="1.0" />
            </StbAccountDataList>

          </HouseInfo>


          <HouseInfo accountId="1901" zip="91761" dma="803" timeZoneLabel="">

            <AuthPackageList>
              <Package id="AC"/>
              <Package id="BP"/>
              <Package id="C1"/>
              <Package id="D0"/>
            </AuthPackageList>

            <StbAccountDataList>
              <AccountData stbId="987458" clientVersion="1.0" />
              <AccountData stbId="978459" clientVersion="1.0" />
            </StbAccountDataList>

          </HouseInfo>

          <HouseInfo accountId="1902" zip="64080" dma="616" timeZoneLabel="">

            <AuthPackageList>
              <Package id="AB"/>
              <Package id="BK"/>
            </AuthPackageList>

            <StbAccountDataList>
              <AccountData stbId="987460" clientVersion="1.0" />
              <AccountData stbId="987461" clientVersion="1.0" />
            </StbAccountDataList>

          </HouseInfo>
       </AccountDataList>
    </ABC>



    Please could any one help, I want some generic away to write a program......
    I am really appreciate your help.
    thanks in advance.

    thanks
    Babu



    sridhar
    • Moved by Peter Ritchie Monday, June 2, 2008 7:12 PM more appropriate forum
    Monday, June 2, 2008 1:47 PM

Answers

All replies

  • I'm having a hardtime understanding your problem/requirements:

    XML is nothing more than text (but in a specific format)... Do you have problems with conversion of the format to another format? (For that purpose people usally take advantage of xsl).

    Tim Van Wassenhove - Please remember to mark the replies as answers if they help.
    Monday, June 2, 2008 2:04 PM
  • What is the output format you are looking for?  It is possible to devise a generic solution, but you need to be specific on the output format.
    Sasha Goldshtein | http://blogs.microsoft.co.il/blogs/sasha
    Monday, June 2, 2008 4:48 PM
  • For questions and discussions about the processing of XML, XSLT and/or XSD using .NET frameworks, LINQ to XML, and the XML Tools in Visual Studio please see http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=38&SiteID=1
    http://www.peterRitchie.com/blog
    Monday, June 2, 2008 7:11 PM
  • thanks for your reply.
    My projet is developed on .txt files and now we are getting in XML format.
    So i have to convet XML to .txt files before running my application.
    and  this is sample xml file and i have to convert original xml file is 4to 5 gb data.

    <ABC>
        <AccountDataList>
          <HouseInfo accountId="1900" zip="55416" dma="613" timeZoneLabel="">

            <AuthPackageList>
              <Package id="AC"/>
              <Package id="BF"/>
            </AuthPackageList>

            <StbAccountDataList>
              <AccountData stbId="987456" clientVersion="1.0" />
              <AccountData stbId="987457" clientVersion="1.0" />
            </StbAccountDataList>

          </HouseInfo>


          <HouseInfo accountId="1901" zip="91761" dma="803" timeZoneLabel="">

            <AuthPackageList>
              <Package id="AC"/>
              <Package id="BP"/>
              <Package id="C1"/>
              <Package id="D0"/>
            </AuthPackageList>

            <StbAccountDataList>
              <AccountData stbId="987458" clientVersion="1.0" />
              <AccountData stbId="978459" clientVersion="1.0" />
            </StbAccountDataList>

          </HouseInfo>

          <HouseInfo accountId="1902" zip="64080" dma="616" timeZoneLabel="">

            <AuthPackageList>
              <Package id="AB"/>
              <Package id="BK"/>
            </AuthPackageList>

            <StbAccountDataList>
              <AccountData stbId="987460" clientVersion="1.0" />
              <AccountData stbId="987461" clientVersion="1.0" />
            </StbAccountDataList>

          </HouseInfo>
       </AccountDataList>
    </ABC>


    The output is are in .txt file.

    1.HouseInfo

    accountId ZIP dma
    1900  55416 613
    1901  91761 803
    1902  64080 616


    2. AuthPackageList

    accountId Package
    1900  AC
    1900  BF
    1901  AC
    1901  BP
    1901  C1
    1901  D0
    1902  AB
    1902  BK

    3. StbAccountDataList

    accountId stbId
    1900  987456
    1900  987457 
    1901  987458
    1901  987459
    1902  987460
    1902  98461 








    sridhar
    Monday, June 2, 2008 7:14 PM