Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create FileParse.csx #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions XML/FileParse.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

var data = (from nodes in XElement.Load("schema.xml").Descendants()
select new
{
Name = nodes.Attributes("name"),
Ref = nodes.Attributes("ref")
});

var list = new List<string>();

foreach (var item in data)
{
if (item.Name.FirstOrDefault() != null) list.Add(item.Name.FirstOrDefault().Value);
if (item.Ref.FirstOrDefault() != null) list.Add(item.Ref.FirstOrDefault().Value);
}

foreach (var item in list.Distinct().OrderBy(i => i.ToString())) {
Console.WriteLine(item);
}
15 changes: 15 additions & 0 deletions XML/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Parsing XML (with output to file) with scriptcs

## Running the sample
* Make sure scriptcs is [installed](https://github.com/scriptcs/scriptcs-samples/blob/master/README.md)
* Make sure FileParse.csx and schema.xml files are in the same folder.
* Run "scriptcs FileParse.csx > output.txt" in command prompt.
* Output.txt file should appear in the same folder as the script and schema files.
* Remove "> output.txt" from command to output result to screen.

## Comments

The need for this example came about when we needed a working proof of concept of
command line driven linq query our DBAs could use as part of SQL job chain. We have data
stored in XML form and needed a flexible parsing solution that can be edited without
the need to compile anything that does the parsing.
43 changes: 43 additions & 0 deletions XML/schema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org" targetNamespace="http://www.w3.org" elementFormDefault="qualified">
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" ref="Id" />
<xs:element minOccurs="0" maxOccurs="1" ref="id" />
<xs:element minOccurs="0" maxOccurs="1" ref="UpdateCount" />
<xs:element minOccurs="0" maxOccurs="1" ref="UpdateUser" />
<xs:element minOccurs="0" maxOccurs="1" ref="UpdateTimestamp" />
<xs:element minOccurs="0" maxOccurs="1" ref="CustomerRef" />
<xs:element minOccurs="0" maxOccurs="1" ref="AccountRef" />
<xs:element minOccurs="0" maxOccurs="1" ref="ExternalSystemInd" />
<xs:element minOccurs="0" maxOccurs="1" ref="Version" />
<xs:element minOccurs="0" maxOccurs="1" ref="VIPLevel" />
<xs:element minOccurs="0" maxOccurs="1" ref="AuditAccountRef" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="QuestionReplies" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Fee" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="AI" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Attachment" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="RiskGroup" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Location" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Line" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="AuditData" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Document" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="LossHistory" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="BillingEntity" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Form" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="DataReportRequest" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="ChangeInfo" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="PartyInfo" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Output" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Note" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="PendingRenewalChanges" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="BasicPolicy" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Insured" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="PolicyReinsurance" />
<xs:element minOccurs="0" maxOccurs="1" ref="StatementAccountRef" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="FLDMVPendingTransactions" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>