C# WinForm how to format a form programmatically only - c#

Currently I am working on generate a form (WinForm) programmatically according to the given format file. This feature is used for generating forms like product specifications.
The given format file is a text file, its extension is unknown yet, it could be .txt. This format file might change in the future according to the needs, for now, it looks like:
[Hardware]
[Voltage]
[Range]
[Accuracy]
[Current]
[Range]
[Time]
[Precision]
[Software]
[Set up]
[A]
[B]
[C]
[Require]
[E]
[F]
...
And my program should read this format file and generate a form accordingly, like:
This form would later be filled with data from a given Excel file, but I can worry about this part later, for now I only need to worry about how to read from the format file and generate a form.
Any tip would be super helpful. Thank you in advance!
Edit 1: the format file can be anything, I am using .xml file for the format now:
<regionList>
<region id="1">
<regionName>Hardware</regionName>
<modules>
<module1>Voltage</module1>
<components>
<component1>Range</component1>
<component2>Accuracy</component2>
</components>
<module2>Current</module2>
<components>
<component1>Range</component1>
<component2>Time</component2>
<component3>Precision</component3>
</components>
</modules>
<\region>
<region id="2">
<regionName>Software</regionName>
<modules>
<module1>Set Up</module1>
<components>
<component1>A</component1>
<component2>B</component2>
<component3>C</component3>
</components>
<module2>Require</module2>
<components>
<component1>D</component1>
<component2>E</component2>
</components>
</modules>
<\region>
...
<\regionList>

Related

Use svcutil for generating C# Classes from simple xml file

I have following XML. I am trying to generate C# Classes using svcutil. I understand that there are other tools (like Visual Studio Paste Special and XML2CSharp) – but I need to see how svcutil is generating it.
<?xml version="1.0"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" templateId="2.16.840.1.113883.3.27.1776">
<title>Gc test Consultation Note</title>
<effectiveTime value="20000407"/>
<component>
<StructuredBody>
<component>
<section>
<code code="10164-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
<title>History of Present Illness</title>
<text>Test patient is a 67 year old male referred for further asthma management. Onset of asthma in his
<content revised="delete">twenties</content>
<content revised="insert">teens</content>.
He was hospitalized twice last year, and already twice this year. He has not been able to be weaned off steroids for the past several months.
</text>
</section>
</component>
</StructuredBody>
</component>
</ClinicalDocument>
I used svcutil articles and used following command – but it gave a warning saying that no code is generated.
svcutil.exe /target:code /dataContractOnly /serializer:XmlSerializer /importXmlTypes /collectionType:System.Collections.Generic.List`1 hl7sample.xml
What can done to generate C# Classes from this XML using svcutil?

Reading in a simple XML config file in C#

Im having a hard time remembering how to do this, and most of the examples I'm seeing dont really cover my issue. I'm trying to read in the below XML file, so that if a user selects a Tool Type from a drop-down menu, the variables for said tool will populate a form on screen. I just have no clue how to collect all the elements/attributes for a specific tool.
<?xml version="1.0" encoding="UTF-8"?>
<Tool_menu>
<tool name="A">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>Confg1</name>
</configuration>
<configuration>
<name>Confg2</name>
</configuration>
<configuration>
<name>Confg3</name>
</configuration>
<configuration>
<name>Confg4</name>
</configuration>
</configuration_list>
</tool>
<tool name="B">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>n/a</name>
</configuration>
<configuration>
<name>n/a</name>
</configuration>
</configuration_list>
</tool>
<tool name="C">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>200Scope</name>
</configuration>
<configuration>
<name>300Scope</name>
</configuration>
<configuration>
<name>600Scope</name>
</configuration>
<configuration>
<name>900Scope</name>
</configuration>
</configuration_list>
</tool>
</Tool_menu>
what I'd want is for a user to select 'tool C' and see a list of configurations available on tool C, name of the tool, and a dropdown of options for who to email (low/high severity) that would be tool specific
**using .net 4.5
Access nodes using XPath.
Take a look at some tutorials here or here
In your case, accessing tools C can be achieved like this:
XmlDocument doc = new XmlDocument();
doc.Load(#"c:\temp\tools.xml");
var toolCemails = doc.SelectNodes("//tool[#name='C']/emails/email"); //two nodes with email tag
var toolCconfigs = doc.SelectNodes("//tool[#name='C']/configuration_list/configuration"); //four config nodes
You could take a look at this post on SO: LINQ to read XML
edit: also, in the comments below your question, #zx485 posted a link to another helpful SO post, here: Bind XML data to a Dropdownlist c#
That might help get you started. If not, please try to narrow your question to something more specific - preferably with some code that we can help you with.
As your question is written right now, it seems that you are asking us to do this for you rather than help you with a specific question.

How to capture data at run time and store it in XML file in WPF?

I am building an application in which the output XML file should look something like this.
<xml file>
<Session>
<Machine name= XXX>
<date = ####>
<Recorded Speech= XXXXXXXX>
<Capture= picture.jpeg>
</Capture>
</Recorded Speech>
</date>
</Machine>
</Session>
<Session>
<Machine name= XXX>
<date = ####>
< Recorded Speech= XXXXXXXX>
<Capture= picture.jpeg>
</Capture>
</Recorded Speech>
</date>
</Machine>
</Session>
</xml file>
and so on...
I am able to output all the data specified in the tags separately but not able to integrate them into a single XML file.
Also, I want to maintain the sessions. The data which is there in the XML is captured during run time.
I am trying to build this in WPF and using MVVM architecture.

How can I compare two XML files for their differences?

I want to compare two xml raw files and capture their differences in data. For example,
<Segment>
<SegNum>2</SegNum>
<Dt>20110910</Dt>
<Comment>B test</Comment>
</Segment>
</Retrieve>
</info>
File B:
<info>
<Retrieve>
<LastNameInfo>
<LNameNum>2</LNameNum>
<NameType/>
<LName>TESTING</LName>
</LastNameInfo>
<Segment>
<SegNum>1</SegNum>
<Status>HS</Status>
<Comment>A test</Comment>
</Segment>
<Segment>
<SegNum>2</SegNum>
<Dt>20110</Dt>
<Comment>COULD I USE LINQ</Comment>
</Segment>
</Retrieve>
</info>
so the output i need should be a file with just the differences like
<LNameNum>1</LnameNum> File 2:<Lnamenum>2</Lnamenum>
There is a library (and tool), XML Diff and Patch, published to MSDN a while back that does this.
I've used the XmlDiff class in the past. At last check, it still works with framework 4.0.
http://msdn.microsoft.com/en-us/library/aa302295.aspx

Could not find schema information for the element 'castle'

I'm creating a Custom tag in my web.config. I first wrote the following entry under the configSections section.
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
But, when I try to create a castle node inside the configuration node as below
<castle>
<components>
</components>
</castle>
I get the following error message:"*Could not find schema information for the element '**castle'*." "***Could not find schema information for the element '**components'***."
Am I missing something? I can't find why. And, if I run the application anyway, I get the following error "Could not find section 'Castle' in the configuration file associated with this domain."
Ps.// The sample comes from "Pro ASP.NET MVC Framework"/Steven Sanderson/APress ISBN-13 (pbk): 978-1-4302-1007-8" on page 99.
Thank you for the help
============================================================
Since I believe to have done exactly what's said in the book and did not succed, I ask the same question in different terms. How do I add a new node using the above information?
=============================================================================
Thank you. I did what you said and do not have the two warnings. However, I've go a big new warning:
"The element 'configuration' in namespace 'MyWindsorSchema' has invalid child element 'configSections' in namespace 'MyWindsorSchema'. List of possible elements expected: 'include, properties, facilities, components' in namespace 'MyWindsorSchema'."
What you get is not an error that will prevent you from running your application. It is just a warning that Visual Studio emits because it does not know the castle node in a config file. You could use a schema to enable intellisense. Download the Castle Windsor Schema file and take a look at the readme.txt inside. It tells you to put windsor.xsd somewhere on your hard drive and then reference it in the config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="MyWindsorSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="MyWindsorSchema file://S:\Common\Windsor\windsor.xsd">
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
</components>
</castle>
</configuration>

Categories