Click here to Skip to main content
15,879,535 members

LINQ TO XML should return data

nelsonmarian asked:

Open original thread
<pre lang="c#">
XML
<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>
Tags: XML, LINQ

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900