Interaction VS and other program - c#

I've written a DLL that parses XML and returns Dictionary with tag name and its value. I'm using it in other program called ZennoPoster Project Maker. Here's the code:
XMLWork.XMLWorker worker = new XMLWork.XMLWorker(); // My parse class
string path = #"Z:\New\test.xml";
Dictionary<string, string> data = worker.GetData(path); // GetData - method, that returns
// data from XML
project.Variables["second_name"].Value = data["second_name"];
This block of code I must remake into XMLWorker class method and return a project type and in ZennoPoster I have to with 1 line of code return data. How can I do it?

Assuming that you have the following simplified types:
namespace Objects
{
public class Project
{
public Dictionary<string, Variable> Variables { get; set; }
}
public class Variable
{
public object Value { get; set; }
}
}
You could structure your XMLWorker class like this:
using Objects;
public class XMLWorker
{
public Project Project { get; private set; }
public XMLWorker(string path)
{
Project = new Project();
Dictionary<string, string> data = GetData(path);
Project.Variables["second_name"].Value = data["second_name"];
}
internal Dictionary<string, string> GetData(string path)
{
// method implementation
}
}
Sample usage:
var project = (new XMLWork.XMLWorker(#"Z:\New\test.xml")).Project;

Related

Create JSON Dynamically using variables containing element names with Dot notation

I have 2 csv files in Azure Blob storage, I am using C# to parse these files
The first csv file is a small file that contains a mapping from the data in the 2nd csv file to an API The records look like the below
csvField1, apiField1.subfield1
csvField2, apiField2
csvField3, apiField5
csvField6, apiField1.subfield2
the second csv file is big so I will use stream to read it and the file has a header with the following column names
csvField1, csfField2, csvField4, csfField5, csvField6, csfField7
I want the output to be a JSON like the below
{
apiField1:{
subfield1: value(csvField1)
subfield2: value(csvField6)
},
apiField2:value(csvField2),
apiField5: value(csvField3)
}
The workable solution with Newtonsoft.Json library:
A) Create data class for apiField1
class ApiField1
{
public ApiField1(string s1, string s2)
{
subfield1 = s1;
subfield2 = s2;
}
public string subfield1 { get;}
public string subfield2 { get;}
}
B) Create data class for API record
class ApiRecord
{
public ApiRecord(string[] s)
{
apiField1 = new ApiField1(s[0], s[5]);
apiField2 = s[1];
apiField5 = s[2];
}
public ApiField1 apiField1 { get; }
public string apiField2 { get; }
public string apiField5 { get; }
}
C) Test
class Program
{
static void Main(string[] args)
{
ApiRecord a = new ApiRecord("0,1,2,3,4,5".Split(','));
Console.WriteLine(JsonConvert.SerializeObject(a));
Console.ReadLine();
}
}
Result:
{"apiField1":{"subfield1":"0","subfield2":"5"},"apiField2":"1","apiField5":"2"}
I just test with a simple string "0,1,2,3,4,5". In your case, you can read each line from the stream of the CSV file.
Or, you can use a dictionary:
Dictionary<string, String> apidield1 = new Dictionary<string, string>();
apidield1.Add("subfield1", "value(csvField1)");
apidield1.Add("subfield2", "value(csvField6)");
Dictionary<string, Object> apiRecord = new Dictionary<string, object>();
apiRecord.Add("apiField2", "value(csvField2)");
apiRecord.Add("apiField5", "value(csvField3)");
apiRecord.Add("apiField1", apidield1);
Console.WriteLine(JsonConvert.SerializeObject(apiRecord));
Output:
{"apiField2":"value(csvField2)","apiField5":"value(csvField3)","apiField1":{"subfield1":"value(csvField1)","subfield2":"value(csvField6)"}}

How to get the base class name of a class via Roslyn?

I am using Roslyn and I have the following class:
var source = #"
using System;
class MyClass : MyBaseClass {
static void Main(string[] args) {
Console.WriteLine(""Hello, World!"");
}
}";
// Parsing
SyntaxTree tree = CSharpSyntaxTree.ParseText(source);
// This uses an internal function (working)
// That gets the first node of type `SimpleBaseTypeSyntax`
SimpleBaseTypeSyntax simpleBaseType = GetNBaseClassNode(tree);
Getting the base class name
I successfully get access to node SimpleBaseTypeSyntax which contains what I need. In fact, if I use the syntax explorer, I get:
Node IdentifierToken has everything I need has its Text, Value and ValueText properties are "MyBaseClass"!
However, while in the syntax explorer I can see all these values, I cannot access them programmatically.
So I try retrieving the node programmatically:
IdentifierNameSyntax identifierNode =
simpleBaseType.ChildNodes().OfType<IdentifierNameSyntax>().First();
SyntaxToken identifier = simpleBaseType.Identifier;
string name = identifier.Text;
But name is empty string. Same as identifier.Value and identifier.ValueText.
What am I doing wrong? Maybe I am doing wrong, so how would you retrieve the base class name?
Another attempt: Using the semantic model
I started thinking that I need the semantic model for this type of information:
IdentifierNameSyntax identifierNode =
simpleBaseType .ChildNodes().OfType<IdentifierNameSyntax>().First();
SemanticModel semanticModel =
CSharpCompilation.Create("Class")
.AddReferences(MetadataReference.CreateFromFile(
typeof(object).Assembly.Location))
.AddSyntaxTrees(tree).GetSemanticModel(tree);
SymbolInfo symbolInfo = this.semanticModel.GetSymbolInfo(identifierNode);
string name = symbolInfo.Symbol.Name;
This throws exception as symbolInfo.Symbol is null.
I actually don't know why you can't pass the BaseTypeSyntax to the semantic model via GetSymbolInfo(), but it's also returning null for me with no errors.
Anyways, here's an approach that works:
var tree = CSharpSyntaxTree.ParseText(#"
using System;
class MyBaseClass
{
}
class MyClass : MyBaseClass {
static void Main(string[] args) {
Console.WriteLine(""Hello, World!"");
}
}");
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var myClass = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().Last();
var myClassSymbol = model.GetDeclaredSymbol(myClass) as ITypeSymbol;
var baseTypeName = myClassSymbol.BaseType.Name;
You'll want to use the semantic model here because you won't be able to reliably tell if you're dealing with an interface or a base type at the syntax level.
I can see that you're trying to build an analyzer with the Roslyn API.
You do know that there are other ways to test your analyzer logic? Using unit test files instead of directly having the source inside the analyzer.
Using this idea, you entirely build your analyzer with the template provided by Visual Studio, where you must inherit from DiagnosticAnalyzer and you create your analysis code logic.
For your situation, you should look at ClassDeclaration and easily access the BaseTypes property inside the Node.
public bool SomeTriedDiagnosticMethod(SyntaxNodeAnalysisContext nodeContext)
{
var classDeclarationNode = nodeContext.Node as ClassDeclarationSyntax;
if (classDeclarationNode == null) return false;
var baseType = classDeclarationNode.BaseList.Types.FirstOrDefault(); // Better use this in all situations to be sure code won't break
var nameOfFirstBaseType = baseType.Type.ToString();
return nameOfFirstBaseType == "BaseType";
}
protected static bool IsWebPage(SyntaxNodeAnalysisContext context, ClassDeclarationSyntax classDeclaration)
{
INamedTypeSymbol iSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration) as INamedTypeSymbol;
INamedTypeSymbol symbolBaseType = iSymbol?.BaseType;
while (symbolBaseType != null)
{
if (symbolBaseType.ToString() == "System.Web.UI.Page")
return true;
symbolBaseType = symbolBaseType.BaseType;
}
return false;
}
This is my helper class that finds all properties, class name, class namespace and base classes.
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace MyProject
{
public class CsharpClass
{
public string Name { get; set; }
public string Namespace { get; set; }
public List<CsharpProperty> Properties { get; set; }
public List<string> BaseClasses { get; set; }
public class CsharpProperty
{
public string Name { get; set; }
public string Type { get; set; }
public CsharpProperty(string name, string type)
{
Name = name;
Type = type;
}
}
public CsharpClass()
{
Properties = new List<CsharpProperty>();
BaseClasses = new List<string>();
}
}
public static class CsharpClassParser
{
public static CsharpClass Parse(string content)
{
var csharpClass = new CsharpClass();
var tree = CSharpSyntaxTree.ParseText(content);
var members = tree.GetRoot().DescendantNodes().OfType<MemberDeclarationSyntax>();
foreach (var member in members)
{
if (member is PropertyDeclarationSyntax property)
{
csharpClass.Properties.Add(new CsharpClass.CsharpProperty(
property.Identifier.ValueText, property.Type.ToString()));
}
if (member is NamespaceDeclarationSyntax namespaceDeclaration)
{
csharpClass.Namespace = namespaceDeclaration.Name.ToString();
}
if (member is ClassDeclarationSyntax classDeclaration)
{
csharpClass.Name = classDeclaration.Identifier.ValueText;
csharpClass.BaseClasses = GetBaseClasses(classDeclaration).ToList();
}
//if (member is MethodDeclarationSyntax method)
//{
// Console.WriteLine("Method: " + method.Identifier.ValueText);
//}
}
return csharpClass;
}
private static IEnumerable<string> GetBaseClasses(ClassDeclarationSyntax classDeclaration)
{
if (classDeclaration == null)
{
return null;
}
if (classDeclaration.BaseList == null)
{
return null;
}
return classDeclaration.BaseList.Types.Select(x => x.Type.ToString());
}
}
}
Usage:
const string content = #"
namespace Acme.Airlines.AirCraft
{
public class AirCraft
{
public virtual string Name { get; set; }
public virtual int Code { get; set; }
public AirCraft()
{
}
}
}";
var csharpClass = CsharpClassParser.Parse(content);
Console.WriteLine(csharpClass.Name);
Console.WriteLine(csharpClass.Namespace);
Console.WriteLine(csharpClass.Properties.Count);
Console.WriteLine(csharpClass.BaseClasses.Count);

Create observable model for databinding

I have a models that are used for serializing data to xml.
Ex
public class File : IFile
{
[XmlAttribute]
public string ObjectId;
public string OriginalFileName { get; set; }
public string FileName { get; set; }
public int Size { get; set; }
public string Note { get; set; }
public static explicit operator File(FileItem a) // explicit File to FileItem conversion operator
{
File b = new File(); // explicit conversion
b.ObjectId = a.ObjectId;
b.Note = a.Note;
b.FileName = a.FileName;
b.OriginalFileName = a.OriginalFileName;
b.Size = a.Size;
return b;
}
}
These classes are mainly used for reading and saving xml documents inside a worddocument.
Inside the main application i need data to be observable so in this case i use prism and the classes inherits from Notificationobject.
Ex
public class FileItem : NotificationObject, IFile
{
public FileItem()
{
}
public static explicit operator FileItem(File a) // explicit File to FileItem conversion operator
{
FileItem d = new FileItem(); // explicit conversion
d.ObjectId = a.ObjectId;
d.Note = a.Note;
d.FileName = a.FileName;
d.OriginalFileName = a.OriginalFileName;
d.Size = a.Size;
return d;
}
private string _objectid;
public string ObjectId
{
get { return _objectid; }
set
{
if (!value.Equals(_objectid))
{
_objectid = value;
this.RaisePropertyChanged(() => this.ObjectId);
}
.................
So what i do is that i first read xml files into the File class and then i need to convert it into FileItem class and when i want to save it back to xml i need to do the reverse conversion again. This seems a little bit unessesary to me. I could think of a solution where i serialize directly into FileItem but i need to keep File simple as it is defined in a assembly that is used by other components that does not need notificationobject and where prism assemblies will not be installed.
Any ideas on how to simplify this.
You could use AutoMapper to automatically map from one object to the other.

C# problem with object array

I am fairly new to arrays in C# and am used to storing a mass of data in a string and in INI files and then breaking it down into basic arrays using delimiters...so yeh, my knowledge is almost none existent.
My main form class begin this definition:
public CAirportData[] _AirportData; //size not known
This is the method I am using to create the array:
...string[] airports = possibleAirports.Split(','); //size is known
foreach (string airport in airports)
{
string[] rwys = inif.Read(airport, "rwys").Split(':'); //size is known (2)
_AirportData = new CAirportData[] { new CAirportData() { icao=airport, depRwy=rwys[0], arrRwy=rwys[1] } };
}
I know this just boils down to my limited knowledge of objects and arrays. But I can't seem to find anything on the internet that uses this sort of thing. I have tried to combine other peoples code with little success.
I need the _AirportData array to be available outside of the form hence public and declared outside of any methods. I supose the main problem is that I am overwriting array and foreach airport I am creating a new array hence loosing the previous. I had tried moving the ..= new CAirportData[] to all sorts of places but Visual Studio doesn't like it.
Below is the class definition for CAirportData:
public class CAirportData
{
public string icao { get; set; }
public string depRwy { get; set; }
public string arrRwy { get; set; }
public override string ToString()
{
string result = string.Format("ICAO: {0}, Dep: {1}, Arr: {2}", this.icao, this.depRwy, this.arrRwy);
return result;
}
}
public class CMRunways
{
public string icao { get; set; }
public string depRwy { get; set; }
public string arrRwy { get; set; }
}
Many thanks in advance for any help!
What you're looking for is generic List. Change the definition to:
public List<CAirportData> _AirportData = new List<CAirportData>();
Then the code in the loop to:
_AirportData.Add(new CAirportData { icao=airport, depRwy=rwys[0], arrRwy=rwys[1] });
This is what I would do...Create a static class, with a static property (airports) and add a static constructor to load the airports from file at the begining.
public static class Session
{
public static CAirportData[] _AirportData;
static Session()
{
string airports = possibleAirports.Split(",");
foreach (string airport in airports)
{
string[] rwys = inif.Read(airport, "rwys").Split(':'); //size is known (2)
_AirportData = new CAirportData[] { new CAirportData() { icao=airport, depRwy=rwys[0], arrRwy=rwys[1] } };
}
}
}
Now you can access the array anywhere in the project like
MessageBox.Show(Session.CAirportData[0].depRwy);

XML to C# Class Question

Can someone please help me, I have this xml snippet
<?xml version="1.0" encoding="utf-8" ?>
<EmailConfiguration>
<DataBoxID>123</DataBoxID>
<DefaultSendToAddressCollection>
<EmailAddress>email#whereEver.com</EmailAddress>
</DefaultSendToAddressCollection>
</EmailConfiguration>
I want to create a corressponding c# class from this. Before you say - "Just use xsd.exe", the output from Xsd cannot be serialized and deserialized correct, because it generates the class using partial classes.
Please can you tell me how to create this class.... here is the approach I took, but it doesn't work.
public class EmailConfiguration
{
private string dataBoxID;
public string DataBoxID
{
get { return dataBoxID; }
set { dataBoxID = value; }
}
private DefaultSendToAddressCollectionClass defaultSendToAddressCollection;
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection
{
get { return defaultSendToAddressCollection; }
set { defaultSendToAddressCollection = value; }
}
}
And here is the class declaration for the subclass
public class DefaultSendToAddressCollectionClass
{
private string[] emailAddress;
public string[] EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
}
Did you use VS2008's XSD?
Here's the output I got:
c:>xsd email.xml
Writing file 'c:\email.xsd'
c:>xsd email.xsd /c /edb
Writing file 'c:\email.cs'
Generates serializable output:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class EmailConfiguration : object, System.ComponentModel.INotifyPropertyChanged {
private string dataBoxIDField;
private EmailConfigurationDefaultSendToAddressCollection[] defaultSendToAddressCollectionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DataBoxID {
get {
return this.dataBoxIDField;
}
set {
this.dataBoxIDField = value;
this.RaisePropertyChanged("DataBoxID");
}
}
You have two possibilities.
Method 1. XSD tool
Suppose that you have your XML file in this location C:\path\to\xml\file.xml
Open Developer Command Prompt
You can find it in Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools
Or if you have Windows 8 can just start typing Developer Command Prompt in Start screen
Change location to your XML file directory by typing cd /D "C:\path\to\xml"
Create XSD file from your xml file by typing xsd file.xml
Create C# classes by typing xsd /c file.xsd
And that's it! You have generated C# classes from xml file in C:\path\to\xml\file.cs
Method 2 - Paste special
Required Visual Studio 2012+
Copy content of your XML file to clipboard
Add to your solution new, empty class file (Shift+Alt+C)
Open that file and in menu click Edit > Paste special > Paste XML As Classes
And that's it!
Usage
Usage is very simple with this helper class:
using System;
using System.IO;
using System.Web.Script.Serialization; // Add reference: System.Web.Extensions
using System.Xml;
using System.Xml.Serialization;
namespace Helpers
{
internal static class ParseHelpers
{
private static JavaScriptSerializer json;
private static JavaScriptSerializer JSON { get { return json ?? (json = new JavaScriptSerializer()); } }
public static Stream ToStream(this string #this)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(#this);
writer.Flush();
stream.Position = 0;
return stream;
}
public static T ParseXML<T>(this string #this) where T : class
{
var reader = XmlReader.Create(#this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
}
public static T ParseJSON<T>(this string #this) where T : class
{
return JSON.Deserialize<T>(#this.Trim());
}
}
}
All you have to do now, is:
public class JSONRoot
{
public catalog catalog { get; set; }
}
// ...
string xml = File.ReadAllText(#"D:\file.xml");
var catalog1 = xml.ParseXML<catalog>();
string json = File.ReadAllText(#"D:\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();
Here you have some Online XML <--> JSON Converters: Click
Bare minimum working... looks like you are only required to add one attribute.
public class EmailConfiguration
{
public string DataBoxID { get; set; }
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection { get; set; }
}
public class DefaultSendToAddressCollectionClass
{
[XmlElement]
public string[] EmailAddress { get; set; }
}
Using .NET 3.5:
[XmlRoot]
public class EmailConfiguration
{
[XmlElement]
public string DataBoxID { get; set; }
[XmlElement]
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection { get; set; }
}
public class DefaultSendToAddressCollectionClass
{
[XmlElement]
public string[] EmailAddress { get; set; }
}
XSD.EXE is the tool that produces classes specifically for the purpose of XML Serialization. If it produces partial classes, that's because they work for XML Serialization. That's not what your problem is.
Try using XSD.EXE and serializing / deserializing. If you get an exception again, then please catch it and then post the results of ex.ToString().
This class will serialize the way you want. I changed your custom collection to a List and used the XmlArrayItem attribute to specify how each email address would be serialized. There are many such attributes to help you fine tune the serialization process.
[Serializable]
public class EmailConfiguration {
private string dataBoxID;
public string DataBoxID {
get { return dataBoxID; }
set { dataBoxID = value; }
}
private List<string> defaultSendToAddressCollection;
[XmlArrayItem("EmailAddress")]
public List<string> DefaultSendToAddressCollection {
get { return defaultSendToAddressCollection; }
set { defaultSendToAddressCollection = value; }
}
public EmailConfiguration() {
DefaultSendToAddressCollection = new List<string>();
}
}
XML serialization requires attributes. The way I've usually done it is to flag the class itself with [Serializable] and [XmlRoot], then mark up public properties with either [XmlElement], [XmlAttribute] or [NoSerialize].
What specific problem are you having?

Categories

Resources