Does WebBrowser.DocumentText as well contains all frame documents text? - c#

I am not sure if WebBrowser.DocumentText contains only top document source or frames document text also included. Could not find that from MSDN page.

No it does not. I have tried next:
DocumentText:
File.WriteAllText(#"C:\doc.txt", webBrowser1.DocumentText, Encoding.UTF8);
GetElementsByTagName("HTML")
HtmlElement elem;
if (webBrowser1.Document != null)
{
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML");
if (elems.Count == 1)
{
elem = elems[0];
string pageSource = elem.OuterHtml;
File.WriteAllText(#"C:\doc.txt", pageSource, Encoding.UTF8);
}
}
IOleCommandTarget
public void ShowSource()
{
IOleCommandTarget cmdt = null;
object o = null;
object oIE = null;
try {
cmdt = (IOleCommandTarget)this.Document.DomDocument;
cmdt.Exec(cmdGUID, oCommands.ViewSource, 1, o, o);
} catch (Exception ex) {
throw new Exception(ex.Message.ToString(), ex.InnerException);
} finally {
cmdt = null;
}
}
The only way is to go through all frame documents.
Updated If iframe has different url you will get UnauthorizedAccessException when trying to retrieve iframe document

Related

Openxml inserts italics at bookmarks.(several characters in a string are italics)

Here is the code for my work.
public void InsertValue(WordprocessingDocument doc, string bookMark, string txt)
{
try
{
RemoveBookMarkContent(doc, bookMark);
var bmStart = FindBookMarkStart(doc, bookMark);
if (bmStart == null)
return;
var run = new Run();
run.Append(GetRunProperties());
run.Append(new Text(txt));
bmStart.Parent.InsertAfter(run, bmStart);
}
catch (Exception c)
{
//not Exception
}
}
private void RemoveBookMarkContent(WordprocessingDocument doc, string bmName)
{
BookmarkStart bmStart = FindBookMarkStart(doc, bmName);
if (bmStart == null)
return;
BookmarkEnd bmEnd = FindBookMarkEnd(doc, bmStart.Id);
while (true)
{
var run = bmStart.NextSibling();
if (run == null)
{
break;
}
if (run is BookmarkEnd && (BookmarkEnd)run == bmEnd)
{
break;
}
run.Remove();
}
}
There are still several auxiliary classes not written.Work process, first find the bookmark location, delete the content of the bookmark location, and then add it.I've also tried to add one Paragraph to the bookmark location.But that doesn't work.
Document to insert in bookmark eg:露点:U=0.15℃(k=2);相对湿度:U=1.0%RH(k=2).Both u and K must be italics.Any help will be appreciated.Thanks.
I tried a new component.[Spire.Office.][1]
At the beginning, I didn't think of a solution, but I used the global search and replacement to determine whether the search location has bookmarks, which perfectly solved the problem.
Here is the code for my work.
var selection = document.FindAllString("U", false, true);
foreach (var sec in selection)
{
var t = sec.GetAsOneRange();
if (sec.GetAsOneRange()?.Owner?.LastChild?.DocumentObjectType == DocumentObjectType.BookmarkEnd)
{
sec.GetAsOneRange().CharacterFormat.Italic = true;
}
}
I didn't try to do this with openxml, but I think the principle should be consistent.
[1]: https://www.e-iceblue.cn/Buy/Spire-PDF-NET.html

Exception loading the XML

I'm using this code to save and restore the XML values ​​but I'm in trouble . Rescue usually works the problem and when I try to load the XML . I get this exception that in the image.
line 105 : string text = el.Attribute("Text").Value;
void SaveData() {
XDocument xmlDocument = new XDocument(new XElement("Pages"));
List<XElement> xmlPages = new List<XElement>();
foreach(KeyValuePair<string, string> doc in documents)
xmlDocument.Root.Add(
new XElement("Page",
new XAttribute("nodeName", GetNodeName(doc.Key)),
new XAttribute("pageGuid", doc.Key),
new XAttribute("Rtf", doc.Value)));
xmlDocument.Root.Add(
new XElement("TextEdit",
new XAttribute("Text", textBox1.Text)));
xmlDocument.Save(GetPathToFile());
}
void LoadData() {
try {
XDocument xmlDocument = XDocument.Load(GetPathToFile());
rootNode.Nodes.Clear();
documents.Clear();
foreach(XElement el in xmlDocument.Root.Elements()) {
string nodeName = el.Attribute("nodeName").Value;
string pageGuid = el.Attribute("pageGuid").Value;
string rtf = el.Attribute("Rtf").Value;
string text = el.Attribute("Text").Value;
rootNode.Nodes.Add(new DataNode(nodeName, pageGuid));
documents.Add(pageGuid, rtf);
textBox1.Text = text;
}
} catch(Exception ex) {
MessageBox.Show("No data loaded. Check XML file" + ex.ToString());
}
treeList1.RefreshDataSource();
}
The exception is clear: There is not such attribute el.Attribute("Text"), so you can't try to get it's value. Check for attribute existence before getting it's value.
After research could solve the case.
Solution:
void LoadData() {
try {
XDocument xmlDocument = XDocument.Load(GetPathToFile());
rootNode.Nodes.Clear();
documents.Clear();
foreach(XElement el in xmlDocument.Root.Elements()) {
switch(el.Name.LocalName) {
case "Page":
string nodeName = el.Attribute("nodeName").Value;
string pageGuid = el.Attribute("pageGuid").Value;
string rtf = el.Attribute("Rtf").Value;
rootNode.Nodes.Add(new DataNode(nodeName, pageGuid));
documents.Add(pageGuid, rtf);
break;
case "Text":
textEdit1.Text = el.Attribute("text").Value;
break;
}
}
} catch(Exception ex) {
MessageBox.Show("No data loaded. Check XML file");
}
treeList1.RefreshDataSource();
}

Read BuiltInDocumentProperties/CustomDocumentProperties alway null with Word 2010?

I want to read out the BuiltInDocumentProperties/CustomDocumentProperties of an Word document. The following Source always return null :-(
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
.....
private void toolStripMenuItemTmp_Click(object sender, EventArgs e)
{
Word.Application word = new Word.Application();
Word.Document document = word.Documents.Open(#"C:\Users\fillibuster\Desktop\docproperty.docx");
DocumentProperties properties = (DocumentProperties)document.CustomDocumentProperties;
if (properties != null)
{
foreach (Microsoft.Office.Core.DocumentProperty item in properties)
{
MessageBox.Show(item.Name.ToString() + item.Value.ToString());
}
}
else
{
MessageBox.Show("null");
}
}
What's wrong with the source? CustomDocumentProperties and BuiltInDocumentProperties are available and filled in the document!
I had the same issue with .docx document. One way to get through was to forget about type casting and instead keep dynamic and object as types and then the code worked. I suspect that the COM property of a .docx file is not the type described in the MSDN...
So this code captures the raw document's properties and set them in a Dictionary.
try
{
BuiltInDocumentProperties = new Dictionary<string, string>();
var builtinProps = Doc.BuiltInDocumentProperties; // don't strong cast this or you will get null
SetBuiltInProperty(builtinProps, "Title");
SetBuiltInProperty(builtinProps, "Keywords");
}
catch (Exception e)
{
// Ignorer l'erreur
Log.Warn("Erreur inattendue à la lecture des propriétés internes du document", e);
}
IDictionary<string, string> BuiltInDocumentProperties { get; set; }
internal void SetBuiltInProperty(dynamic builtInProps, string property)
{
if (builtInProps != null)
{
try
{
var prop = builtInProps[property];
if (prop != null)
{
string str = prop.Value.ToString();
BuiltInDocumentProperties[property] = str;
}
}
catch (RuntimeBinderException)
{
// Property is missing
}
catch (COMException)
{
}
}
}

Select an option of a html page

Im trying to get my application to select an option out of a page
If looked on site for some help but could not find anything so some help would be nice
My code:
private void sellCars()
{
HtmlElementCollection elements = this.get_mainframe_tags("img");
IEnumerator enumerator2;
elements = this.get_mainframe_tags("option");
try
{
enumerator2 = elements.GetEnumerator();
while (enumerator2.MoveNext())
{
HtmlElement element2 = (HtmlElement)enumerator2.Current;
Console.WriteLine("Test = " + element2.GetAttribute("value").ToString() + "");
if (element2.GetAttribute("value").ToString() == "sell")
{
Console.WriteLine("Called");
element2.SetAttribute("value", "sell");
}
}
}
finally
{
enumerator2 = elements.GetEnumerator();
if (enumerator2 is IDisposable)
{
(enumerator2 as IDisposable).Dispose();
}
}
}

Get url from firefox 8 not working anymore

I have a windows applicaiton c# catching the url of a running firefox instance.
I have always used "MozillaContentWindow" to get firefox URL but i dont understand why it dont work anymore.
string s = GetUrlFromBrowsersWithIdentifier("MozillaContentWindow", foreGround);
public string GetUrlFromBrowsersWithIdentifier(string identifier, int foreground)
{
try
{
IntPtr ptr = new IntPtr(foreground);
var aeBrowser = AutomationElement.FromHandle(ptr);
return aeBrowser == null ? "" : GetURLfromBrowser(aeBrowser, identifier);
}
catch (Exception ex)
{
return "";
}
}
string GetURLfromBrowser(AutomationElement rootElement, string identifier)
{
try
{
Condition condition1 = new PropertyCondition(AutomationElement.IsContentElementProperty, true);
Condition condition2 = new PropertyCondition(AutomationElement.ClassNameProperty, identifier);
var walker = new TreeWalker(new AndCondition(condition1, condition2));
var elementNode = walker.GetFirstChild(rootElement);
if (elementNode != null)
{
var p = elementNode.GetSupportedPatterns();
if (p.Any(autop => autop.ProgrammaticName.Equals("ValuePatternIdentifiers.Pattern")))
{
var valuePattern = elementNode.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
if (valuePattern != null)
return (valuePattern.Current.Value);
}
}
}
catch
{
return "";
}
return "";
}
Now when it enters "walker.GetFirstChild(rootElement);" it just stops there. I cant figure out why. This only happend on latest version of firefox.
Did they change the name of the value bar containing the url?
Thank you
Try using MozillaWindowContentClass for newer versions.

Categories

Resources