Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML LINQ
<pre lang="c#">
<AnswerSet>
    <Answer name="es/21-9-c">
        <TextValue>420 Quincy St.</TextValue>
    </Answer>
    <Answer name="rptmc/programtype">
        <MCValue>
            <SelValue>VS</SelValue>
        </MCValue>
    </Answer>
    <Answer name="programtype">
        <MCValue>
            <SelValue>AS</SelValue>
            <SelValue>BS</SelValue>
            <SelValue>CS</SelValue>
            <SelValue>Ron</SelValue>
            <SelValue>Deepak</SelValue>
            <SelValue>Nelson</SelValue>
        </MCValue>
    </Answer>
</AnswerSet>
 
    public class AnswerFile
        {
        public string txt_DSCL_COLUMN_NAME { get; set; }
        public string int_DSCL_VARIABLE_TYPE_ID { get; set; }
        public DateTime dat_DSCL_CREATED_DATE { get; set; }
        public string txt_DSVL_VALUE { get; set; }
        } 
 

 string getXmlPath = @"C:\DA_Portal\ClientFiles\3977034278\DownloadedDataSets\10-16-12\Transformed\";
            System.Collections.Generic.List<fileinfo> _theFiles;
            _theFiles = Utility.GetFiles(getXmlPath, "xml");
           
            int totalFiles = _theFiles.Count;
            foreach (FileInfo AnswerFileInfo in _theFiles)
                {
 
                XDocument obAnsFile;
                string AnswerFileName = AnswerFileInfo.Name;
                obAnsFile = new XDocument();
                obAnsFile = XDocument.Load(AnswerFileInfo.FullName);
 
                 
 

                IEnumerable<answerfile> Answers = (from e in obAnsFile.Descendants("Answer")
                                                   select new AnswerFile
                                                   {
                                                       txt_DSCL_COLUMN_NAME = e.Attribute("name").Value,
                                                       txt_DSVL_VALUE = (string)e.Element("MCValue").Value.Insert(e.Element("MCValue").Value.Length, "|"),
                                                       dat_DSCL_CREATED_DATE = DateTime.Now,
                                                       int_DSCL_VARIABLE_TYPE_ID = e.FirstNode != null ? ((XElement)e.FirstNode).Name.LocalName : "hello",
 
                                                   }).ToList();
 
The Output I want is should be in this format
 
For this
 
<answer name="es/21-9-c">
        <textvalue>420 Quincy St.</textvalue>
</answer>
 
txt_DSCL_COLUMN_NAME ="es/21-9-c"
int_DSCL_VARIABLE_TYPE_ID ="TextValue"
dat_DSCL_CREATED_DATE="26/10/2012"
txt_DSVL_VALUE ="420 Quincy St."
 
--------------------------------------------
 
 <answer name="rptmc/programtype">
        <mcvalue>
            <selvalue>VS</selvalue>
        </mcvalue>
    </answer>
 
txt_DSCL_COLUMN_NAME  ="rptmc/programtype"
int_DSCL_VARIABLE_TYPE_ID ="MCValue"
dat_DSCL_CREATED_DATE="26/10/2012"
txt_DSVL_VALUE ="VS"
 
<answer name="programtype">
        <mcvalue>
            <selvalue>AS</selvalue>
            <selvalue>BS</selvalue>
            <selvalue>CS</selvalue>
            <selvalue>Ron</selvalue>
            <selvalue>Deepak</selvalue>
            <selvalue>Nelson</selvalue>
        </mcvalue>        
    </answer>   
 
txt_DSCL_COLUMN_NAME  ="programtype"
int_DSCL_VARIABLE_TYPE_ID ="MCValue"
dat_DSCL_CREATED_DATE="26/10/2012"
txt_DSVL_VALUE ="VS|BS|CS|Ron|Deepak|Nelson"
 
Kindly let me know if the things are not clear</answerfile></fileinfo>
Posted 26-Oct-12 2:14am
Comments
Marcus Kramer at 26-Oct-12 12:16pm
   
Great. And what's your question?
nelsonmarian at 29-Oct-12 1:34am
   
Good Morning Marcus Kramer
 
Thanks for reply. My question how to get | when there are more child nodes and no | when there is no child node or only one child node.MY xml will also have TextValue ,NumValue,DateValue,TFValue,DocTextValue. They will not have child elements but MCValue , ClauseLibValue DBValue .But they will have atleast one child nodes. Will your solution will be faster.because I am having 10,000 files and each file contains around 1000 records.
 


806584


01/01/2010

806584


<TFValue>false</TFValue>



VS




AS
BS
CS
Ron
Deepak
Nelson



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100