0

I need to create xml by reading the pdf file names from a location.I wrote code but cannt get the iterative child nodes.Please help me for the same

Sample file names : ABC_2345_P1.pdf ABC_2111_Q1.pdf ABC_1225_Q2.pdf

The Out XML should require like below :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Folder>
    <PolicyFrom>ABC</PolicyFrom>
    <Documents>
        <Doc>
            <DocGuid>2345</DocGuid>
                        <DocType>P1</DocType>
                        <DocName>ABC_2345_P1.pdf</DocName>

        </Doc>
        <Doc>
            <DocGuid>2111</DocGuid>
                        <DocType>Q1</DocType>
                        <DocName>ABC_2111_Q1.pdf</DocName>
    </Doc>
        <Doc>
            <DocGuid>1225</DocGuid>
                        <DocType>Q2</DocType>
                        <DocName>ABC_1225_Q2.pdf</DocName>
    </Doc>
    </Documents>
</Folder>

I wrote a code but it dont create iterative child nodes,Please help me to rectify:

For i As Integer = 1 To _NoOfFiles
                        'Dim FileNameSeq = _DocIssueDate & "_" & _DocTypeCode & "_" & _PolicyNo & "_" & _CompanyCode & "_" & _Case_id & "_" & _NoOfFiles & "_" & _FileNo & "_" & _PStartDate
                        Dim FileNameSeq = _PolicyFrom & "_" & _DocGuid & "_" & _DocType & "_" & _DocName

                        If File.Exists(_SOURCEPDFPATHWITHOUTFILE & "\" & FileNameSeq & ".pdf") Then
                            Dim FileLocation As DirectoryInfo = New DirectoryInfo("C:\Folder\Subfolder\Data\Input\")
                            Dim FileLoc As String = "C:\Folder\Subfolder\Data\Input\"
                            'Dim fi As FileInfo() = FileLocation.GetFiles(_DocIssueDate & "_" & _DocTypeCode & "_" & _PolicyNo & "_" & _CompanyCode & "_" & _Case_id & "_" & _NoOfFiles & "_" & i & "_" & _PStartDate & ".pdf")
                            For Each file As String In IO.Directory.GetFiles(_SOURCEPDFPATHWITHOUTFILE, "" _PolicyFrom & "_" & _DocGuid & "_" & _DocType & "_" & _DocName & ".pdf", IO.SearchOption.AllDirectories)
                                'The next line of code gets file names with extensions from searched directories and subdirectories
                                Dim fName As String = IO.Path.GetFileName(file)
                                Console.WriteLine(fName)
                                If i = 1 Then
                                    Dim Firstfile As String = fName
                                    Dim companyFile As XElement = New XElement("Folder",
                                    New XElement("PolicyFrom", New Object() ,
                                    New XElement("Doc",
                                    New XElement("DocGuid", _SOURCEPDFPATH_FileName,
                                    New XElement("DocType", _SOURCEPDFPATH_FileName,
                                    New XElement("DocName", _SOURCEPDFPATH_FileName,
                                           ))))
                            })
                    })
                    )

                                    companyFile.Save("D:\PDF_UPLD\" & _SOURCEPDFPATH_FileName & ".xml")

                                End If
                                'System.IO.File.Delete(FileLoc & fName)
                            Next

                        End If
                    Next


                Else

                    Continue For
                End If


            Next
vim
  • 824
  • 3
  • 12
  • 27
  • Check if [this](https://stackoverflow.com/questions/59074005/saving-listbox-with-additional-information-in-my-settings/59097544#59097544) and [this](https://stackoverflow.com/questions/58686509/trouble-serializing-and-deserializing-multiple-objects/58687570#58687570) could give you some tips and hints. –  Dec 16 '19 at 05:23
  • @JQSOFT i did some modification and now i can able to add new child node but only issue is that it adding child on wrong place. – vim Dec 16 '19 at 08:33
  • Edit your question and add that code to take a look if you'd like. –  Dec 16 '19 at 16:26

1 Answers1

1

This is some self contained code creating the xml structure you need:

var xDocument = new XDocument(new XElement("Folder"));

xDocument.Root.Add(new XElement("PolicyFrom", "ABC"));

var documents = new XElement("Documents");

for(var i = 0; i < 3; i++)
{
    var doc = new XElement("Doc");

    doc.Add(new XElement("DocGuid", "2345"));

    doc.Add(new XElement("DocType", "P1"));

    doc.Add(new XElement("DocName", "ABC_2345_P1.pdf"));

    documents.Add(doc);
}

xDocument.Root.Add(documents);

All you have to do is replace the for loop with yours, and populate the xml nodes with the correct file information.

Kai Hartmann
  • 3,106
  • 1
  • 31
  • 45
  • So you mean to say ,i have to read all file names that is data. and add the nodes. then at the end save the xml .is it?because my data is coming from file names, i am reading all one by one. – vim Dec 16 '19 at 11:01
  • You can still read the files one by one. Just replace the for-loop. It's important though, that you don't create nodes 'PolicyFrom' and 'Documents' inside the loop, otherwise they will also be repeated. – Kai Hartmann Dec 16 '19 at 11:35
  • @ Kai Hartmann so saving of the xml will be at the end after reading the last file? – vim Dec 16 '19 at 11:41
  • 1
    Yes, outside the loop. – Kai Hartmann Dec 16 '19 at 11:47