本文介绍了解析v。大型嵌套xml文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I’m using the attached c# code to parse the attached xml file. The problem is that within each organization tag there are multiple organizationName tags. This is because organizations change their names over time. The code is only getting the most recent whereas I would like all the history.
Ideally, I’d also like to be able to filter on specific elements and/or attributes. However this is lower priority as I can just iterate through the file created by the code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq ;
using System.IO;
namespace ParseXML
{
public partial class Form1 : Form
{
string origId = "4295856130";
string typeName = "LNG";
string effectiveTo = "2005-08-18T04:00:00";
string[] AryContributors = new string[100];
long contributorCt = 0;
//based on http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx
string ContributorFile = "C:\\OAOrganization\\OrgPermIDs.txt";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// The values to filter on
// The File OrigIds.txt in this case ontains one OriginazationId per line
// If you have a different format in the file you will need to extract the ID's
// so that you have one ID per element of the array or List<>
string[] AryContributors = File.ReadAllLines(ContributorFile);
try
{
using (StreamWriter sw = new StreamWriter("C:\\OAOrganization\\OrganizationNameParsed.txt"))
{
sw.WriteLine("OrganizationID|entityCreatedDate|entityModifiedDate|OrganizationName|OrganizationName_effectiveFrom|OrganizationName_effectiveTo|OrganizationName_organizationNameTypeCode|organizationName_LanguageID|OrganizationName_organizationNameLocalNormalized");
//XElement doc = XElement.Load(@"C:\OAOrganization\OrganizationNameFull.txt");
string csv = (from el in SimpleStreamAxis(@"C:\OAOrganization\File2.txt", "Organization")
//string csv = (from el in doc.Descendants()
let ns = String.Format("{{{0}}}", el.Name.NamespaceName)
//where el.Name.LocalName == "Organization" && el.Element(ns + "OrganizationId").Value == AryContributors [i].ToString ()
//where el.Name.LocalName == "Organization" //&& AryContributors.Contains(el.Element(ns + "OrganizationId").Value)
select String.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}",
(string)el.Element(ns + "OrganizationId"),
(string)el.Attribute ( "entityCreatedDate"),
(string)el.Attribute("entityModifiedDate"),
(string)el.Element(ns + "OrganizationName"),
(string)el.Element(ns + "OrganizationName").Attribute("effectiveFrom"),
(string)el.Element(ns + "OrganizationName").Attribute("effectiveTo"),
(string)el.Element(ns + "OrganizationName").Attribute("organizationNameTypeCode"),
(string)el.Element(ns + "OrganizationName").Attribute("languageId"),
(string)el.Element(ns + "OrganizationName").Attribute("organizationNameLocalNormalized"),
Environment.NewLine
)
)
.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString()
);
sw.WriteLine(csv);
}
}
catch (Exception ex)
{
lblErrs.Text = ex.Message;
}
//GC.Collect;
Environment.Exit(0);
}
private IEnumerable<XElement> SimpleStreamAxis(
string filename, string matchName)
{
using (XmlTextReader reader = new XmlTextReader(filename))
{
reader.MoveToContent();
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.LocalName == matchName)
{
XElement el = XElement.ReadFrom(reader)
as XElement;
if (el != null)
yield return el;
}
break;
}
}
reader.Close();
}
}
}
}
<?xml version='1.0' encoding='utf-8'?><env:ContentEnvelope majVers='3' minVers='2.3' pubStyle='Incremental' xmlns:env='http://data.schemas.tfn.thomson.com/Envelope/2008-05-01/' xmlns:cmn='http://data.schemas.financial.thomsonreuters.com/Common/2009-09-01/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://oa.schemas.tfn.thomson.com/Organization/2010-05-01/' xsi:schemaLocation='http://oa.schemas.tfn.thomson.com/Organization/2010-05-01/ OrganizationMaster.xsd'><env:Header><env:Info><env:Id>urn:uuid:843a1d51-445a-403e-b062-88430a6aff26</env:Id><env:TimeStamp>2015-01-19T13:54:23</env:TimeStamp></env:Info></env:Header><env:Body majVers='3' minVers='3.0' contentSet='OA'>
<env:ContentItem action="Overwrite"><env:Data xsi:type="OrganizationDataItem"><Organization entityCreatedDate="2009-07-22T00:48:57" entityModifiedDate="2015-01-19T05:27:10" isOrganizationVerified="true" isOrganizationManaged="true" isOrganizationFinancialLocalBranch="false" isOrganizationFinancialForeignBranch="false" isOrganizationOtherBranchandDivision="false" entityLastReviewedDate="2014-05-01T04:00:00" organizationTier="1"><OrganizationId>4295904866</OrganizationId><AdminStatus effectiveFrom="2009-07-22T00:48:57">Published</AdminStatus><OrganizationName effectiveFrom="1988-01-01T05:00:00" effectiveTo="2014-04-25T04:00:00" organizationNameTypeCode="FKA" languageId="505062" organizationNameLocalNormalized="S Y Bancorp Inc" sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/835324/000115752314001592/a50851040.htm" sourceUpdateDate="2014-05-01T09:07:06">S. Y. BANCORP, INC.</OrganizationName><OrganizationName effectiveFrom="2014-04-25T04:00:00" organizationNameTypeCode="LNG" languageId="505062" organizationNameLocalNormalized="Stock Yards Bancorp Inc" sourceType="4" sourceLink="https://app.sos.ky.gov/ftshow/(S(fuibso3ik0fpgwtjm4azfzf4))/default.aspx?path=ftsearch&id=0238669&ct=09&cs=99998" sourceUpdateDate="2014-05-01T09:01:56">STOCK YARDS BANCORP, INC.</OrganizationName><OrganizationName effectiveFrom="2014-04-25T04:00:00" organizationNameTypeCode="SHT" languageId="505062">Stock Yards</OrganizationName><OrganizationAddress addressTypeCode="LREG" languageId="505062" sourceType="4" sourceLink="https://app.sos.ky.gov/ftshow/(S(fuibso3ik0fpgwtjm4azfzf4))/default.aspx?path=ftsearch&id=0238669&ct=09&cs=99998" sourceUpdateDate="2014-05-01T09:01:56"><OrganizationAddressLine1>SUITE 1800</OrganizationAddressLine1><OrganizationAddressLine2>400 WEST MARKET ST.</OrganizationAddressLine2><OrganizationAddressLine3 naCode="NA02" /><OrganizationAddressCityCode>24194</OrganizationAddressCityCode><OrganizationAddressCity>LOUISVILLE</OrganizationAddressCity><OrganizationAddressStateProvince>KENTUCKY</OrganizationAddressStateProvince><OrganizationAddressStateProvinceCode>2032</OrganizationAddressStateProvinceCode><OrganizationAddressStateProvinceOfficialCode>KY</OrganizationAddressStateProvinceOfficialCode><OrganizationAddressPostalCode>40202-3352</OrganizationAddressPostalCode><OrganizationAddressCountryCode>US</OrganizationAddressCountryCode></OrganizationAddress><OrganizationAddress addressTypeCode="HEA" languageId="505062" sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/835324/000110465914035516/0001104659-14-035516.txt" sourceUpdateDate="2014-05-07T13:46:00"><OrganizationAddressLine1 naCode="NA02" /><OrganizationAddressLine2>1040 E MAIN ST</OrganizationAddressLine2><OrganizationAddressLine3 naCode="NA02" /><OrganizationAddressCityCode>24194</OrganizationAddressCityCode><OrganizationAddressCity>LOUISVILLE</OrganizationAddressCity><OrganizationAddressStateProvince>KENTUCKY</OrganizationAddressStateProvince><OrganizationAddressStateProvinceCode>2032</OrganizationAddressStateProvinceCode><OrganizationAddressStateProvinceOfficialCode>KY</OrganizationAddressStateProvinceOfficialCode><OrganizationAddressPostalCode>40206</OrganizationAddressPostalCode><OrganizationAddressCountryCode>US</OrganizationAddressCountryCode></OrganizationAddress><OrganizationPhone sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/835324/000110465914035516/0001104659-14-035516.txt" sourceUpdateDate="2014-05-07T13:46:00" organizationPhoneTypeCode="ORG_HQ_ADD_PHONE">15025822571</OrganizationPhone><OrganizationPhone organizationPhoneTypeCode="ORG_HQ_ADD_FAX" naCode="NA02" /><OrganizationPhone organizationPhoneTypeCode="ORG_REG_ADD_PHONE" naCode="NA02" /><OrganizationPhone organizationPhoneTypeCode="ORG_REG_ADD_FAX" naCode="NA02" /><OrganizationWebsite sourceType="15" sourceLink="http://www.snl.com/irweblinkx/corporateprofile.aspx?iid=100548" sourceUpdateDate="2014-03-10T19:50:53">http://www.snl.com/irweblinkx/corporateprofile.aspx?iid=100548</OrganizationWebsite><IsPublicFlag>true</IsPublicFlag><OrganizationFoundedYear sourceType="4" sourceLink="https://app.sos.ky.gov/ftshow/(S(fuibso3ik0fpgwtjm4azfzf4))/default.aspx?path=ftsearch&id=0238669&ct=09&cs=99998" sourceUpdateDate="2014-05-01T09:01:56">1988</OrganizationFoundedYear><OrganizationFoundedMonth sourceType="4" sourceLink="https://app.sos.ky.gov/ftshow/(S(fuibso3ik0fpgwtjm4azfzf4))/default.aspx?path=ftsearch&id=0238669&ct=09&cs=99998" sourceUpdateDate="2014-05-01T09:01:56">01</OrganizationFoundedMonth><OrganizationFoundedDay sourceType="4" sourceLink="https://app.sos.ky.gov/ftshow/(S(fuibso3ik0fpgwtjm4azfzf4))/default.aspx?path=ftsearch&id=0238669&ct=09&cs=99998" sourceUpdateDate="2014-05-01T09:01:56">12</OrganizationFoundedDay><OrganizationTypeCode sourceType="15" sourceLink="http://www.snl.com/irweblinkx/corporateprofile.aspx?iid=100548" sourceUpdateDate="2014-05-01T09:01:56">COM</OrganizationTypeCode><OrganizationStatusCode>Act</OrganizationStatusCode><OrganizationProviderTypeCode>1</OrganizationProviderTypeCode><OrganizationJurisdictionOfIncorporationTypeCode sourceType="4" sourceLink="https://app.sos.ky.gov/ftshow/(S(fuibso3ik0fpgwtjm4azfzf4))/default.aspx?path=ftsearch&id=0238669&ct=09&cs=99998" sourceUpdateDate="2014-05-01T09:01:56">451</OrganizationJurisdictionOfIncorporationTypeCode><OrganizationJurisdictionOfIncorporation>KENTUCKY</OrganizationJurisdictionOfIncorporation><OrganizationSubtypeCode sourceType="15" sourceLink="http://www.snl.com/irweblinkx/corporateprofile.aspx?iid=100548" sourceUpdateDate="2014-05-01T09:01:56">FHC</OrganizationSubtypeCode></Organization></env:Data></env:ContentItem><env:ContentItem action="Overwrite"><env:Data xsi:type="OrganizationDataItem"><Organization entityCreatedDate="2009-07-21T11:36:32" entityModifiedDate="2015-01-19T05:24:54" isOrganizationVerified="true" isOrganizationManaged="true" isOrganizationFinancialLocalBranch="false" isOrganizationFinancialForeignBranch="false" isOrganizationOtherBranchandDivision="false" entityLastReviewedDate="2014-03-12T04:00:00" organizationTier="1"><OrganizationId>4295904882</OrganizationId><AdminStatus effectiveFrom="2009-07-21T11:36:32">Published</AdminStatus><OrganizationName effectiveFrom="1993-01-01T05:00:00" organizationNameTypeCode="LNG" languageId="505062" organizationNameLocalNormalized="Saul Centers Inc" sourceType="4" sourceLink="http://sdat.resiusa.org/ucc-charter/Pages/CharterSearch/default.aspx" sourceUpdateDate="2014-08-27T05:49:11">SAUL CENTERS, INC.</OrganizationName><OrganizationName effectiveFrom="1993-01-01T05:00:00" organizationNameTypeCode="SHT" languageId="505062">Saul Centers</OrganizationName><OrganizationAddress addressTypeCode="LREG" languageId="505062" sourceType="4" sourceLink="http://sdat.resiusa.org/ucc-charter/Pages/CharterSearch/default.aspx" sourceUpdateDate="2014-08-27T09:11:07"><OrganizationAddressLine1>351 W Camden St</OrganizationAddressLine1><OrganizationAddressLine2 naCode="NA02" /><OrganizationAddressLine3 naCode="NA02" /><OrganizationAddressCityCode>3394</OrganizationAddressCityCode><OrganizationAddressCity>BALTIMORE</OrganizationAddressCity><OrganizationAddressStateProvince>MARYLAND</OrganizationAddressStateProvince><OrganizationAddressStateProvinceCode>2035</OrganizationAddressStateProvinceCode><OrganizationAddressStateProvinceOfficialCode>MD</OrganizationAddressStateProvinceOfficialCode><OrganizationAddressPostalCode>21201</OrganizationAddressPostalCode><OrganizationAddressCountryCode>US</OrganizationAddressCountryCode></OrganizationAddress><OrganizationAddress addressTypeCode="HEA" languageId="505062" sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/907254/000090725414000033/0000907254-14-000033.txt" sourceUpdateDate="2014-03-17T14:31:40"><OrganizationAddressLine1>SUITE 1500</OrganizationAddressLine1><OrganizationAddressLine2>7501 WISCONSIN AVENUE</OrganizationAddressLine2><OrganizationAddressLine3 naCode="NA02" /><OrganizationAddressCityCode>4616</OrganizationAddressCityCode><OrganizationAddressCity>BETHESDA</OrganizationAddressCity><OrganizationAddressStateProvince>MARYLAND</OrganizationAddressStateProvince><OrganizationAddressStateProvinceCode>2035</OrganizationAddressStateProvinceCode><OrganizationAddressStateProvinceOfficialCode>MD</OrganizationAddressStateProvinceOfficialCode><OrganizationAddressPostalCode>20814</OrganizationAddressPostalCode><OrganizationAddressCountryCode>US</OrganizationAddressCountryCode></OrganizationAddress><OrganizationPhone sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/907254/000090725414000033/0000907254-14-000033.txt" sourceUpdateDate="2014-03-17T14:31:40" organizationPhoneTypeCode="ORG_HQ_ADD_PHONE">13019866207</OrganizationPhone><OrganizationPhone sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/907254/000090725414000033/0000907254-14-000033.txt" sourceUpdateDate="2014-03-17T14:31:40" organizationPhoneTypeCode="ORG_HQ_ADD_FAX">13019866079</OrganizationPhone><OrganizationPhone organizationPhoneTypeCode="ORG_REG_ADD_PHONE" naCode="NA02" /><OrganizationPhone organizationPhoneTypeCode="ORG_REG_ADD_FAX" naCode="NA02" /><OrganizationWebsite sourceType="15" sourceLink="http://www.saulcenters.com/" sourceUpdateDate="2010-04-30T04:00:00">http://www.saulcenters.com/</OrganizationWebsite><IsPublicFlag>true</IsPublicFlag><IpoDate>1993-08-19T04:00:00</IpoDate><OrganizationFoundedYear sourceType="4" sourceLink="http://sdat.resiusa.org/ucc-charter/Pages/CharterSearch/default.aspx" sourceUpdateDate="2014-09-01T06:34:56">1993</OrganizationFoundedYear><OrganizationFoundedMonth sourceType="4" sourceLink="http://sdat.resiusa.org/ucc-charter/Pages/CharterSearch/default.aspx" sourceUpdateDate="2014-09-01T06:34:56">06</OrganizationFoundedMonth><OrganizationFoundedDay sourceType="4" sourceLink="http://sdat.resiusa.org/ucc-charter/Pages/CharterSearch/default.aspx" sourceUpdateDate="2014-09-01T06:34:56">10</OrganizationFoundedDay><OrganizationTypeCode sourceType="15" sourceLink="http://www.saulcenters.com/ourcompany.htm" sourceUpdateDate="2009-07-21T04:00:00">CINV</OrganizationTypeCode><OrganizationStatusCode>Act</OrganizationStatusCode><OrganizationProviderTypeCode>1</OrganizationProviderTypeCode><OrganizationJurisdictionOfIncorporationTypeCode sourceType="4" sourceLink="http://sdat.resiusa.org/ucc-charter/Pages/CharterSearch/default.aspx" sourceUpdateDate="2014-08-28T09:26:06">454</OrganizationJurisdictionOfIncorporationTypeCode><OrganizationJurisdictionOfIncorporation>MARYLAND</OrganizationJurisdictionOfIncorporation><OrganizationSubtypeCode sourceType="15" sourceLink="http://www.saulcenters.com/ourcompany.htm" sourceUpdateDate="2009-07-21T04:00:00">REIT</OrganizationSubtypeCode></Organization></env:Data></env:ContentItem><env:ContentItem action="Overwrite"><env:Data xsi:type="OrganizationDataItem"><Organization entityCreatedDate="2009-07-21T22:04:17" entityModifiedDate="2015-01-19T05:29:09" isOrganizationVerified="true" isOrganizationManaged="true" isOrganizationFinancialLocalBranch="false" isOrganizationFinancialForeignBranch="false" isOrganizationOtherBranchandDivision="false" entityLastReviewedDate="2014-10-22T04:00:00" organizationTier="1"><OrganizationId>4295904889</OrganizationId><AdminStatus effectiveFrom="2009-07-21T22:04:17">Published</AdminStatus><OrganizationName effectiveFrom="1976-01-01T05:00:00" effectiveTo="1997-04-28T04:00:00" organizationNameTypeCode="FKA" languageId="505062" organizationNameLocalNormalized="Schuff Steel Co" sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/354204/000089843007002660/0000898430-07-002660.txt" sourceUpdateDate="2014-10-22T02:27:06">SCHUFF STEEL CO</OrganizationName><OrganizationName effectiveFrom="1997-04-28T04:00:00" organizationNameTypeCode="LNG" languageId="505062" organizationNameLocalNormalized="Schuff International Inc" sourceType="4" sourceLink="https://delecorp.delaware.gov/tin/GINameSearch.jsp" sourceUpdateDate="2009-07-21T04:00:00">SCHUFF INTERNATIONAL, INC.</OrganizationName><OrganizationName effectiveFrom="1997-04-28T04:00:00" organizationNameTypeCode="SHT" languageId="505062">Schuff Intl</OrganizationName><OrganizationAddress addressTypeCode="LREG" languageId="505062" sourceType="4" sourceLink="http://corp.delaware.gov/agents/a9000010.shtml" sourceUpdateDate="2011-05-19T04:00:00"><OrganizationAddressLine1>Corporation Trust Center</OrganizationAddressLine1><OrganizationAddressLine2>1209 Orange Street</OrganizationAddressLine2><OrganizationAddressLine2Transliterated>NA02</OrganizationAddressLine2Transliterated><OrganizationAddressLine2English>NA02</OrganizationAddressLine2English><OrganizationAddressLine3>New Castle County</OrganizationAddressLine3><OrganizationAddressLine3Transliterated>NA02</OrganizationAddressLine3Transliterated><OrganizationAddressLine3English>NA02</OrganizationAddressLine3English><OrganizationAddressCityCode>46865</OrganizationAddressCityCode><OrganizationAddressCity>WILMINGTON</OrganizationAddressCity><OrganizationAddressStateProvince>DELAWARE</OrganizationAddressStateProvince><OrganizationAddressStateProvinceCode>2021</OrganizationAddressStateProvinceCode><OrganizationAddressStateProvinceOfficialCode>DE</OrganizationAddressStateProvinceOfficialCode><OrganizationAddressPostalCode>19801</OrganizationAddressPostalCode><OrganizationAddressCountryCode>US</OrganizationAddressCountryCode></OrganizationAddress><OrganizationAddress addressTypeCode="HEA" languageId="505062" sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/354204/000089843007002660/0000898430-07-002660.txt" sourceUpdateDate="2013-12-09T17:31:21"><OrganizationAddressLine1>1841 W Buchanan St</OrganizationAddressLine1><OrganizationAddressLine2 naCode="NA02" /><OrganizationAddressLine3 naCode="NA02" /><OrganizationAddressCityCode>32165</OrganizationAddressCityCode><OrganizationAddressCity>PHOENIX</OrganizationAddressCity><OrganizationAddressStateProvince>ARIZONA</OrganizationAddressStateProvince><OrganizationAddressStateProvinceCode>2015</OrganizationAddressStateProvinceCode><OrganizationAddressStateProvinceOfficialCode>AZ</OrganizationAddressStateProvinceOfficialCode><OrganizationAddressPostalCode>85007-3335</OrganizationAddressPostalCode><OrganizationAddressCountryCode>US</OrganizationAddressCountryCode></OrganizationAddress><OrganizationPhone sourceType="22" sourceLink="http://www.sec.gov/Archives/edgar/data/354204/000089843007002660/0000898430-07-002660.txt" sourceUpdateDate="2013-12-09T17:31:21" organizationPhoneTypeCode="ORG_HQ_ADD_PHONE">16022527787</OrganizationPhone><OrganizationPhone organizationPhoneTypeCode="ORG_HQ_ADD_FAX" naCode="NA02" /><OrganizationPhone sourceType="4" sourceLink="http://corp.delaware.gov/agents/a9000010.shtml" sourceUpdateDate="2011-05-19T04:00:00" organizationPhoneTypeCode="ORG_REG_ADD_PHONE">13026587581</OrganizationPhone><OrganizationPhone sourceType="4" sourceLink="http://corp.delaware.gov/agents/a9000010.shtml" sourceUpdateDate="2011-05-19T04:00:00" organizationPhoneTypeCode="ORG_REG_ADD_FAX">13026555049</OrganizationPhone><OrganizationWebsite sourceType="15" sourceLink="http://www.schuff.com/" sourceUpdateDate="2014-10-22T02:27:06">http://www.schuff.com/</OrganizationWebsite><IsPublicFlag>true</IsPublicFlag><IpoDate>1997-07-01T04:00:00</IpoDate><OrganizationFoundedYear sourceType="4" sourceLink="https://delecorp.delaware.gov/tin/GINameSearch.jsp" sourceUpdateDate="2009-07-21T04:00:00">2001</OrganizationFoundedYear><OrganizationFoundedMonth sourceType="4" sourceLink="https://delecorp.delaware.gov/tin/GINameSearch.jsp" sourceUpdateDate="2009-07-21T04:00:00">06</OrganizationFoundedMonth><OrganizationFoundedDay sourceType="4" sourceLink="https://delecorp.delaware.gov/tin/GINameSearch.jsp" sourceUpdateDate="2009-07-21T04:00:00">29</OrganizationFoundedDay><OrganizationTypeCode sourceType="15" sourceLink="http://www.schuff.com/schuff-international/about-us/" sourceUpdateDate="2014-10-22T02:27:06">COM</OrganizationTypeCode><OrganizationStatusCode>Act</OrganizationStatusCode><OrganizationProviderTypeCode>1</OrganizationProviderTypeCode><OrganizationJurisdictionOfIncorporationTypeCode sourceType="4" sourceLink="https://delecorp.delaware.gov/tin/GINameSearch.jsp" sourceUpdateDate="2009-07-21T04:00:00">440</OrganizationJurisdictionOfIncorporationTypeCode><OrganizationJurisdictionOfIncorporation>DELAWARE</OrganizationJurisdictionOfIncorporation><OrganizationSubtypeCode sourceType="15" sourceLink="http://www.schuff.com/schuff-international/about-us/" sourceUpdateDate="2014-10-22T02:27:06">COR</OrganizationSubtypeCode></Organization></env:Data></env:ContentItem></env:Body></env:ContentEnvelope>
推荐答案
using System;
using System.Xml;
namespace ReadXMLfromFile
{
/// <summary>
/// </summary>
class pdaXMLParser
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader("c:\\temp\\file2.xml");
string csvRoot = "";
string sep = "|";
//write the output file header string (overwrite any existing file)
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\temp\OrganizationNameParsed.txt"))
{
file.WriteLine("OrganizationID|entityCreatedDate|entityModifiedDate|OrganizationName|OrganizationName_effectiveFrom|OrganizationName_effectiveTo|OrganizationName_organizationNameTypeCode|organizationName_LanguageID|OrganizationName_organizationNameLocalNormalized");
}
while (reader.Read())
{
// Only detect start elements.
if (reader.IsStartElement())
{
// Get element name and switch on it.
switch (reader.Name)
{
case "Data":
// Detect this element.
//Console.WriteLine("Start <data> element.");
break;
case "Organization":
//start a new csv string for use later...
csvRoot = "";
// Detect the Organization element and extract the required attributes
string attribute = reader["entityCreatedDate"];
if (attribute != null)
{
csvRoot += attribute;
}
else { csvRoot += sep; }
attribute = reader["entityModifiedDate"];
if (attribute != null)
{
//Console.WriteLine(" entityModifiedDate: " + attribute);
csvRoot += sep + attribute;
}
else { csvRoot += sep; }
break;
case "OrganizationId":
// Detect the Organization element and extract the required data from the next record
if (reader.Read())
{
//Console.WriteLine(" Organization ID: " + reader.Value.Trim());
//prefix the root data with the OrgID
csvRoot = reader.Value.Trim()+sep + csvRoot;
}
else { csvRoot = sep + csvRoot; }
break;
case "OrganizationName":
// Detect the Organization element and extract the required data from the attributes
//reset the details field as there may be >1 Organization name per OrganizationID
string csvNameDetails = "";
attribute = reader["effectiveFrom"];
if (attribute != null)
{
//Console.WriteLine(" effectiveFrom: " + attribute);
csvNameDetails += sep + attribute;
}
else { csvNameDetails += sep; }
attribute = reader["effectiveTo"];
if (attribute != null)
{
//Console.WriteLine(" effectiveTo: " + attribute);
csvNameDetails += sep + attribute;
}
else
{ csvNameDetails += sep; }
attribute = reader["organizationNameTypeCode"];
if (attribute != null)
{
//Console.WriteLine(" organizationNameTypeCode: " + attribute);
csvNameDetails += sep + attribute;
}
else
{ csvNameDetails += sep; }
attribute = reader["languageId"];
if (attribute != null)
{
//Console.WriteLine(" languageId: " + attribute);
csvNameDetails += sep + attribute;
}
else
{ csvNameDetails += sep; }
attribute = reader["organizationNameLocalNormalized"];
if (attribute != null)
{
//Console.WriteLine(" organizationNameLocalNormalized: " + attribute);
csvNameDetails += sep + attribute;
}
else
{ csvNameDetails += sep; }
// read ahead to get the Organization Name text and prefix this to the attribute data
if (reader.Read())
{
//Console.WriteLine(" Organization ID: " + reader.Value.Trim());
csvNameDetails = reader.Value.Trim() + csvNameDetails ;
}
else { csvNameDetails += sep; }
//write the root details for the OrganizationID along with the current OrganizationName details
// Console.WriteLine(csvRoot+sep+csvNameDetails);
//write the output file data string (append to an existing file)
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\temp\OrganizationNameParsed.txt",true))
{
file.WriteLine(csvRoot + sep+ csvNameDetails);
}
break;
}
}
}
Console.ReadLine();
}
}
}</data>
这篇关于解析v。大型嵌套xml文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!