dynamic content control mapping for MS word c# - c#

I am using code like this:
public void BindControlsToCustomXmlPart()
{
wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
foreach (Word.ContentControl contentControl in wordApp.ActiveDocument.ContentControls)
{
if (contentControl.Tag == "FieldName")
{
string xPathFieldName = "ns:records/ns:record/ns:FieldName";
contentControl.XMLMapping.SetMapping(xPathFieldName,
prefix, currentWordDocumentXMLPart);
}
What ends up happening is every new field I want to add, I have to repeat this redundant code:
if (contentControl.Tag == "FieldName2")
{
string xPathFieldName2 = "ns:records/ns:record/ns:FieldName2";
contentControl.XMLMapping.SetMapping(xPathFieldName2,
prefix, currentWordDocumentXMLPart);
}
Is there a way that I can write this code once and have the "FieldName" portion get updated for each field dynamically? i.e. have some type of loop that would increment through each xmlnode in an xml file (in this case it would map the xml node FieldName to the content control with a tag of FieldName, and then map the xml node FieldName2 to the content control with a tag of FieldName2

A good start would be creating a function to transform your control and reuse that function multiple times as followed
public contentControl BindControlsOperation(contentControl control, string pFieldName)
{
if (control.Tag == pFieldName)
{
string xPathFieldName = String.Format("ns:records/ns:record/ns:{0}",pFieldName);
control.XMLMapping.SetMapping(xPathFieldName,prefix, currentWordDocumentXMLPart);
}
return control;
}
You could then use it in the following fashion
foreach (Word.ContentControl contentControl in wordApp.ActiveDocument.ContentControls)
{
contentControl = BindControlsOperation(contentControl,"FieldName")
}
Next step would be to have a list of names you want to use for fields and feed it to your algorythm using a for loop
....
List<string> names = "x,y,z";
for(int i=0;i < names.length();i++)
{
wordApp.ActiveDocument.ContentControls[i] = BindControlsOperation(wordApp.ActiveDocument.ContentControls[i],name[i])
}
Hope this helps

Related

Cannot find item name C# XML

I'm having a problem with my XML document.
I want my program to find all values of the items in my XML file, but only if the handlingType is of a certain character bunch.
Code (C#) :
string path = "//files//handling.meta";
var doc = XDocument.Load(path);
var items = doc.Descendants("HandlingData").Elements("Item");
var query = from i in items
select new
{
HandlingName = (string)i.Element("handlingName"),
HandlingType = (string)i.Element("HandlingType"),
Mass = (decimal?)i.Element("fMass")
};
foreach (var HandlingType in items)
{
if (HandlingType.ToString() == "HANDLING_TYPE_FLYING")
{
MessageBox.Show(HandlingType.ToString());
}
}
The above code demonstraights a short version of what I want to happen, but fails to find this handlingType (does not show the messageBox)
Here's the XML :
<CHandlingDataMgr>
<HandlingData>
<Item type="CHandlingData">
<handlingName>Plane</handlingName>
<fMass value="380000.000000"/>
<handlingType>HANDLING_TYPE_FLYING</handlingType>
</Item>
<Item type="CHandlingData">
<handlingName>Car1</handlingName>
<fMass value="150000.000000"/>
<handlingType>HANDLING_TYPE_DRIVING</handlingType>
</Item>
</HandlingData>
</CHandlingDataMgr>
I would like the output to show the handlingName if it contains a certain HandlingType
For e.g.
if (handlingType == "HANDLING_TYPE_FLYING")
{
messageBox.Show(this.HandlingName);
}
My problem in short : Program does not find item's handling type, it does find the tag but when asked to display, returns empty/shows as nothing.
Edit: Also in the XML handling_type_flying contains extra elements such as thrust that cannot be found in each item (such as car), I would like the program to also find these elements. (this is a second problem I'm facing, maybe should ask 2nd ques?)
Several things that need fixing.
you are not using your query in your foreach loop. foreach (var item in query)
Your element has an upercase "H" but should be lowercase "handlingType". HandlingType = (string)i.Element("handlingType"),
You are not pulling the Attribute value of your fMass element.Mass = i.Element("fMass").Attribute("value").Value
Once you adjust your Query in your foreach loop you then need to adjust the loop to account for looping over your newly made object.
NOTE that I removed (decimal) from Mass = i.Element("fMass").Attribute("value").Value
here is the code with all the fixes.
class Program
{
static void Main()
{
const string path = "//files//handling.meta";
var doc = XDocument.Load(path);
var items = doc.Descendants("HandlingData").Elements("Item");
var query = from i in items
select new
{
HandlingName = (string)i.Element("handlingName"),
HandlingType = (string)i.Element("handlingType"),
Mass = i.Element("fMass").Attribute("value").Value
};
foreach (var item in query)
{
if (item.HandlingType == "HANDLING_TYPE_FLYING")
{
//Remove messagebox if consoleapp
MessageBox.Show(item.HandlingType);
MessageBox.Show(item.HandlingName);
Console.WriteLine(item.HandlingType);
Console.WriteLine(item.HandlingName);
}
}
}
}
I would recommend looking into serializing your xml to an object.
If you look at http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx the ToString() method doesn't return the name of the tag, but the indented XML.
You should instead be using the Value property. Also you should use .equals("...") instead of ==
if (handlingType.Value.equals("HANDLING_TYPE_FLYING")
{
messageBox.Show(this.handlingname);
}

Wild Card Search in Listbox in C#

In my C# window application I need wild card search on list box.
i.e If I write some text in textbox it should be auto selected in that list box.
List box is binding using datatable e.g lstVendor.datasource = l_dtTable
Findstring() function is finding match only for starting string. But I need if match find at any position in particular text then it should be highlighted.
I am Using below code but not getting index/or even lstVendor.selecteditem = "string" not working.
Indexof() always return -1
string final = "";
foreach (Object lstItem in lstVendor.Items)
{
string s = ((DataRowView)(lstItem)).Row.ItemArray[0].ToString();
if (s.ToLower().Contains(txtVendor.Text.ToLower()))
{
int i = lstVendor.Items.IndexOf(s);
final += s + ",";
}
}
string[] l_strArrVendorList = final.TrimEnd(',').Split(',');
for (int Counter = 0; Counter < l_strArrVendorList.Length; Counter++)
{
lstVendor.SelectedItem = l_strArrVendorList[Counter];
}
Searching may return multiple matched items, this code will find the first matched items:
var firstMatched = listBox1.Items.Cast<DataRowView>()
.Where(v=>Convert.ToString(v.Row[0]).ToLower()
.Contains(txtVendor.Text.ToLower()))
.FirstOrDefault();
if(firstMatched != null) listBox1.SelectedItem = firstMatched;
You can remove the FirstOrDefault() to get the list of matched items and implement some navigation through the matched items.
For VS 2000 support, you have to use this extension class:
public static class EnumerableExtension {
public static IEnumerable<T> Cast<T>(this System.Collections.IEnumerable source){
foreach(var item in source)
yield return (T)item;
}
}
My code above should work OK for you, but looks like you just want your code to be fixed instead, here is the fixed code for you:
//Note that you need to set SelectionMode for your listBox like this:
lstVendor.SelectionMode = SelectionMode.MultiSimple;
foreach (var lstItem in lstVendor.Items.Cast<DataRowView>()) {
string s = lstItem.Row.ItemArray[0].ToString();
if (s.ToLower().Contains(txtVendor.Text.ToLower())) {
lstVendor.SelectedItems.Add(lstItem);
}
}

How do I merge two XDocuments removing duplicates

I have two XML files (*.resx files) that I am trying to merge in to one removing duplicates, but am unable to do so. I've tried the following without any success:
var resource1 = XDocument.Load("C:\\Resources.resx");
var resource2 = XDocument.Load("C:\\Resources2.resx");
// This results in a file with all the nodes from the second file included inside
// the root element of the first file to form a properly formatted, concatenated file.
resource1.Descendants().FirstOrDefault().Add(resource2.Descendants().FirstOrDefault().Nodes());
var nodeContent = new List<string>();
foreach (XElement node in resource1.Root.Elements())
{
if (nodeContent.Contains(node.ToString()))
resource1.Remove();
else
nodeContent.Add(node.ToString());
}
resource1.Save("C:\\FinalResources.resx");
On the remove statement I get an InvalidOperationException - "The parent is missing.":
Am I doing something wrong?
You need to define an EqualityComparer<XElement> that will enable you to use the standard LINQ operators.
So, as a simple example I created this:
public class ElementComparer : EqualityComparer<XElement>
{
public override int GetHashCode(XElement xe)
{
return xe.Name.GetHashCode() ^ xe.Value.GetHashCode();
}
public override bool Equals(XElement xe1, XElement xe2)
{
var #return = xe1.Name.Equals(xe2.Name);
if (#return)
{
#return = xe1.Value.Equals(xe2.Value);
}
return #return;
}
}
So I can then start with these two XML documents:
<xs>
<x>D</x>
<x>A</x>
<x>B</x>
</xs>
<xs>
<x>E</x>
<x>B</x>
<x>C</x>
</xs>
And do this:
var xml1 = XDocument.Parse(#"<xs><x>D</x><x>A</x><x>B</x></xs>");
var xml2 = XDocument.Parse(#"<xs><x>E</x><x>B</x><x>C</x></xs>");
xml1.Root.Add(
xml2.Root.Elements("x")
.Except(xml1.Root.Elements("x"), new ElementComparer()));
Then xml1 will look like this:
<xs>
<x>D</x>
<x>A</x>
<x>B</x>
<x>E</x>
<x>C</x>
</xs>
Well, the most straight forward way is:
var resource1 = XDocument.Load("C:\\Resources.resx");
var resource2 = XDocument.Load("C:\\Resources2.resx");
foreach (XElement node in resource2.Root.Elements())
{
if (resource1.Root.Contains(node)) continue;
resource1.Add(node);
}
resource1.Save("C:\\FinalResources.resx");
public static class XElementExtensions
{
public static bool Contains(this XElement root, XElement e)
{
//or w/e equality logic you need
return root.Elements().Any(x => x.ToString().Equals(e.ToString()));
}
}
This will only merge first level entries tho. If you need deep merge, then you will have to set up a simple recursion (using the same loop for child elements).
resource1.Remove(); is called twice and what it does is remove the root element. So the second time there is no longer a root element to remove from and thus throwing the exception.

How can I improve the speed of loading xml documents

I need to load 2 types of xml documents; one has 50 sub-children and the other has the same 50 and 800 additional ones. Performance is great with the smaller doc and acceptable with the larger doc until the number of children increases. 20k children * 50 sub-children = great performance, 20k children * 850 sub-children = slow performance. How would I skip looking for the extra descendants when they do not exist? My initial attempts lead me to think I need have separate classes, methods, viewmodels, and views for both the small and large docs. Below is a condensed look at my code.
public class MyItem
{
private string layout;
private string column;
private string columnSpan;
private string row;
private string rowSpan;
private string background;
public MyItem(string layout, string column, string columnSpan, string row, string rowSpan, string background)
{
Layout = layout;
Column = column;
ColumnSpan = columnSpan;
Row = row;
RowSpan = rowSpan;
Background = background;
}
public string Layout
{
get { return this.layout; }
set { this.layout = value; }
}
(Not Shown - Column, ColumnSpan, Row, RowSpan, and Background which are handled the same way as Layout)
Just for this example, below shows only 6 sub-children, I am looking for a way to load xml docs with only the first 2 sub-children. This way I can use whatever load method is required by both small or large xml docs.
internal class myDataSource
{
//Loads (MyList) xml file
public static List<MyItem> Load(string MyListFilename)
{
var myfiles = XDocument.Load(MylistFilename).Descendants("item").Select(
x => new MyItem(
(string)x.Element("layout"),
(string)x.Element("column"),
(string)x.Element("columnSpan"),
(string)x.Element("row"),
(string)x.Element("rowSpan"),
(string)x.Element("background")));
return myfiles.ToList();
}
public class MainViewModel : ViewModelBase
{
public void LoadMyList()
{
this.myfiles = new ObservableCollection<MyItemViewModel>();
List<MyItem> mybaseList = myDataSource.Load(MyListFilename);
foreach (MyItem myitem in mybaseList)
{
this.myfiles.Add(new MyItemViewModel(myitem));
}
this.mycollectionView = (ICollectionView)CollectionViewSource.GetDefaultView(myfiles);
if (this.mycollectionView == null)
throw new NullReferenceException("mycollectionView");
}
}
public class MyItemViewModel: ViewModelBase
{
private Models.MyItem myitem;
public MyItemViewModel(MyItem myitem)
{
if (myitem == null)
throw new NullReferenceException("myitem");
this.myitem = myitem;
}
public string Layout
{
get
{
return this.myitem.Layout;
}
set
{
this.myitem.Layout = value;
OnPropertyChanged("Layout");
}
}
(Not Shown - Column, ColumnSpan, Row, RowSpan, and Background which are handled the same way as Layout)
Instead of using Descendants, can you follow the direct path (i.e. use Elements)? That's the only way you'll keep from scanning nodes you know don't have items.
i think one thing you can do is not do a toList on the Select and keep it as lazy, and return an Iterable instead, or whatever Select returns.(sorry, i don't have a windows box right now to test this on). when you do the foreach, you will iterate over it only once (instead of twice right now)
XDocument is handy, but if the problem is simply that the files are large and you only have to scan once through, XmlReader might be the better choice. It doesn't read the entire file, it reads one node at a time. You can manually skip through parts you are not interested in.

How to get values from tag? [duplicate]

I am developing a Windows Forms application which is interacting with a web site.
Using a WebBrowser control I am controlling the web site and I can iterate through the tags using:
HtmlDocument webDoc1 = this.webBrowser1.Document;
HtmlElementCollection aTags = webDoc1.GetElementsByTagName("a");
Now, I want to get a particular text from the tag which is below:
Show Assigned<br>
Like here I want to get the number 244 which is equal to assignedto in above tag and save it into a variable for further use.
How can I do this?
You can try splitting a string by ';' values, and then each string by '=' like this:
string aTag = ...;
foreach(var splitted in aTag.Split(';'))
{
if(splitted.Contains("="))
{
var leftSide = splitted.Split('=')[0];
var rightSide = splitted.Split('=')[1];
if(leftSide == "assignedto")
{
MessageBox.Show(rightSide); //It should be 244
//Or...
int num = int.Parse(rightSide);
}
}
}
Other option is to use Regexes, which you can test here: www.regextester.com. And some more info on regexes: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
Hope it helps!
If all cases are similar to this and you don't mind a reference to System.Web in your Windows Forms application, tou can do something like this:
using System;
public class Program
{
static void Main()
{
string href = #"issue?status=-1,1,2,3,4,5,6,7&
#sort=-activity&#search_text=&#dispname=Show Assigned&
#filter=status,assignedto&#group=priority&
#columns=id,activity,title,creator,status&assignedto=244&
#pagesize=50&#startwith=0";
href = System.Web.HttpUtility.HtmlDecode(href);
var querystring = System.Web.HttpUtility.ParseQueryString(href);
Console.WriteLine(querystring["assignedto"]);
}
}
This is a simplified example and first you need to extract the href attribute text, but that should not be complex. Having the href attribute text you can take advantage that is basically a querystring and reuse code in .NET that already parses query strings.
To complete the example, to obtain the href attribute text you could do:
HtmlElementCollection aTags = webBrowser.Document.GetElementsByTagName("a");
foreach (HtmlElement element in aTags)
{
string href = element.GetAttribute("href");
}

Categories

Resources