/// <inheritdoc /> protected override XmlDocumentFragment GenerateXmlDocumentFragment(XmlDocument xml) { // What should the colour of the progress bar be? var backgroundColour = "green"; var numberColour = "#666"; if (this.TaskState.HasValue && this.TaskState.Value == MFTaskState.MFTaskStateFailed ) { backgroundColour = "red"; numberColour = "red"; } // Create the basic structure of the progress bar. XmlDocumentFragment fragment = null; if (this.PercentageComplete.HasValue) { // We have a percentage complete, so render a progress bar. fragment = DashboardHelper.CreateFragment(xml, $"<div class='progress-bar' style='min-width: 200px; background-color: white; position: relative; width: 100%; height: 23px; border: 1px solid #CCC; padding: 2px 5px 5px 5px; border-radius: 2px; box-sizing: border-box; overflow: hidden; font-size: 10px;'>" + $"<div class='number' style='position: absolute; top: 0px; left: 0px; bottom: 0px; width: 30px; padding: 2px 5px; text-align: center; background-color: #EEE; color: {numberColour}'>{this.PercentageComplete.Value}%</div>" + $"<div class='bar' style='position: absolute; width: {this.PercentageComplete.Value}%; bottom: 0px; background-color: {backgroundColour}; left: 0px; height: 4px'></div>" + $"<div class='text' style='position: absolute; top: 0px; bottom: 5px; left: 40px; right: 0px; white-space: nowrap; overflow: hidden; padding: 2px 5px; text-overflow: ellipsis; color: {numberColour}'></div>" + "</div>"); XmlElement progressBarElement = (XmlElement)fragment.SelectNodes("div[@class='progress-bar']")[0]; progressBarElement.SetAttribute("title", this.Text); XmlElement text = (XmlElement)progressBarElement.SelectNodes("*[@class=\"text\"]")[0]; text.InnerText = this.Text; } else { // Render an indeterminate progress bar. fragment = DashboardHelper.CreateFragment(xml, $"<div class='progress-bar' style='min-width: 200px; position: relative; width: 100%; height: 20px; border: 1px solid #CCC; padding: 2px 5px; border-radius: 2px; box-sizing: border-box; overflow: hidden; font-size: 10px; background-color: {backgroundColour}; background-repeat: repeat; background-position: center center; background-size: 40px 40px; background-image: linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent); color: white;text-overflow: ellipsis; animation: progress-bar-stripes 1s linear infinite;'></div>"); ((XmlElement)fragment.FirstChild).SetAttribute("title", this.Text); fragment.FirstChild.InnerText = this.Text; } // Get a handle on the various elements. XmlElement progressBar = (XmlElement)fragment.SelectNodes("div[@class=\"progress-bar\"]")[0]; //// Append any commands defined for the item. //if (this.Commands != null) //{ // foreach (DashboardCommand cmd in this.Commands) // cmdBar.AppendChild(cmd.Generate(xml)); //} return(fragment); }
private static void organizeXML(MqttMsgPublishEventArgs e) { String topic = e.Topic; String xml = Encoding.UTF8.GetString(e.Message); XmlDocument doc = new XmlDocument(); try { if (!(e.Topic).Equals("alarms")) { if (!File.Exists("param-data.xml")) { doc.AppendChild(doc.CreateElement("sensors")); doc.Save("param-data.xml"); } doc.Load("param-data.xml"); XmlNode root = doc.DocumentElement; XmlDocumentFragment xmlDocFragment = doc.CreateDocumentFragment(); xmlDocFragment.InnerXml = xml; root.AppendChild(xmlDocFragment); doc.Save("param-data.xml"); } else { if (!File.Exists("alarms-data.xml")) { doc.AppendChild(doc.CreateElement("alarms")); doc.Save("alarms-data.xml"); } doc.Load("alarms-data.xml"); XmlNode root = doc.DocumentElement; XmlDocumentFragment aux = doc.CreateDocumentFragment(); aux.InnerXml = xml; XmlNodeList alarms = aux.SelectNodes("/alarm/trigger"); foreach (XmlNode alarm in alarms) { root.AppendChild(alarm); } doc.Save("alarms-data.xml"); } } catch (Exception ex) { throw ex; } }
private static IEnumerable ExtractNodes(XmlNode element, string includeFileName) { XmlDocument ownerDocument = element is XmlDocument ? element as XmlDocument : element.OwnerDocument; XmlDocumentFragment fragment = ownerDocument.CreateDocumentFragment(); try { //XmlDocument includeDoc = ChoXmlDocument.Load(includeFileName); XmlDocument includeDoc = new XmlDocument(_nameTable); includeDoc.Load(includeFileName); foreach (XmlAttribute attr in element.Attributes) { if (attr.Name != PathToken && attr.Name != CinchoNSToken) { includeDoc.DocumentElement.SetAttribute(attr.Name, attr.Value); } } fragment.InnerXml = includeDoc.InnerXml; return(new XmlNode[] { fragment.SelectSingleNode("/*") }); } catch (XmlException) { try { using (StreamReader sr = File.OpenText(includeFileName)) fragment.InnerXml = sr.ReadToEnd(); return(fragment.SelectNodes("child::node()" /*"/*|/comment()"*/)); } catch (Exception innerEx) { throw new ChoXmlDocumentException(String.Format("{0}: Error loading xml file.", includeFileName), innerEx); } } catch (Exception ex) { throw new ChoXmlDocumentException(String.Format("{0}: Error loading xml file.", includeFileName), ex); } }
public static void Main() { XmlDocument doc = new XmlDocument(); string xmlnodestr = @"<mynode value1='1' value2='123'>abc</mynode> <mynode value1='1' value2='123'>abc</mynode> <mynode value1='1' value2='123'>abc</mynode>"; XmlDocumentFragment frag = doc.CreateDocumentFragment(); frag.InnerXml = xmlnodestr; XmlNodeList nodes = frag.SelectNodes("*"); foreach (XmlNode node in nodes) { Console.WriteLine(node.Name + " value1 = {0}; value2 = {1}", node.Attributes["value1"].Value, node.Attributes["value2"].Value); } }
// parameters: // strItemBarcodeParam 册条码号。可以使用 @refID: 前缀 public static int ReturnChangeReaderAndItemRecord( string strAction, string strItemBarcodeParam, string strReaderBarcode, XmlDocument domLog, string strRecoverComment, ref XmlDocument readerdom, ref XmlDocument itemdom, out string strError) { strError = ""; if (strAction != "return" && strAction != "lost") { strError = "ReturnChangeReaderAndItemRecord() 只能处理 strAction 为 'return' 和 'lost' 的情况,不能处理 '" + strAction + "'"; return(-1); } string strReturnOperator = DomUtil.GetElementText(domLog.DocumentElement, "operator"); string strOperTime = DomUtil.GetElementText(domLog.DocumentElement, "operTime"); // *** 修改读者记录 string strDeletedBorrowFrag = ""; XmlNode dup_reader_history = null; // 既然日志记录中记载的是 @refID: 的形态,那读者记录中 borrows 里面势必记载的也是这个形态 XmlNode nodeBorrow = readerdom.DocumentElement.SelectSingleNode( "borrows/borrow[@barcode='" + strItemBarcodeParam + "']"); if (nodeBorrow != null) { if (String.IsNullOrEmpty(strRecoverComment) == false) { string strText = strRecoverComment; string strOldRecoverComment = DomUtil.GetAttr(nodeBorrow, "recoverComment"); if (String.IsNullOrEmpty(strOldRecoverComment) == false) { strText = "(借阅时原注: " + strOldRecoverComment + ") " + strRecoverComment; } DomUtil.SetAttr(nodeBorrow, "recoverComment", strText); } strDeletedBorrowFrag = nodeBorrow.OuterXml; nodeBorrow.ParentNode.RemoveChild(nodeBorrow); // 获得几个查重需要的参数 XmlDocument temp = new XmlDocument(); temp.LoadXml(strDeletedBorrowFrag); string strItemBarcode = temp.DocumentElement.GetAttribute("barcode"); string strBorrowDate = temp.DocumentElement.GetAttribute("borrowDate"); string strBorrowPeriod = temp.DocumentElement.GetAttribute("borrowPeriod"); dup_reader_history = readerdom.DocumentElement.SelectSingleNode("borrowHistory/borrow[@barcode='" + strItemBarcode + "' and @borrowDate='" + strBorrowDate + "' and @borrowPeriod='" + strBorrowPeriod + "']"); } // 加入到读者记录借阅历史字段中 if (string.IsNullOrEmpty(strDeletedBorrowFrag) == false && dup_reader_history == null) { // 看看根下面是否有 borrowHistory 元素 XmlNode root = readerdom.DocumentElement.SelectSingleNode("borrowHistory"); if (root == null) { root = readerdom.CreateElement("borrowHistory"); readerdom.DocumentElement.AppendChild(root); } XmlDocumentFragment fragment = readerdom.CreateDocumentFragment(); fragment.InnerXml = strDeletedBorrowFrag; // 插入到最前面 XmlNode temp = DomUtil.InsertFirstChild(root, fragment); // 2007/6/19 if (temp != null) { // returnDate 加入还书时间 DomUtil.SetAttr(temp, "returnDate", strOperTime); // borrowOperator string strBorrowOperator = DomUtil.GetAttr(temp, "operator"); // 把原来的operator属性值复制到borrowOperator属性中 DomUtil.SetAttr(temp, "borrowOperator", strBorrowOperator); // operator 此时需要表示还书操作者了 DomUtil.SetAttr(temp, "operator", strReturnOperator); } // 如果超过100个,则删除多余的 while (root.ChildNodes.Count > 100) { root.RemoveChild(root.ChildNodes[root.ChildNodes.Count - 1]); } // 2007/6/19 // 增量借阅量属性值 string strBorrowCount = DomUtil.GetAttr(root, "count"); if (String.IsNullOrEmpty(strBorrowCount) == true) { strBorrowCount = "1"; } else { long lCount = 1; try { lCount = Convert.ToInt64(strBorrowCount); } catch { } lCount++; strBorrowCount = lCount.ToString(); } DomUtil.SetAttr(root, "count", strBorrowCount); } // 增添超期信息 string strOverdueString = DomUtil.GetElementText(domLog.DocumentElement, "overdues"); if (String.IsNullOrEmpty(strOverdueString) == false) { XmlDocumentFragment fragment = readerdom.CreateDocumentFragment(); fragment.InnerXml = strOverdueString; List <string> existing_ids = new List <string>(); // 看看根下面是否有overdues元素 XmlNode root = readerdom.DocumentElement.SelectSingleNode("overdues"); if (root == null) { root = readerdom.CreateElement("overdues"); readerdom.DocumentElement.AppendChild(root); } else { // 记载以前已经存在的 id XmlNodeList nodes = root.SelectNodes("overdue"); foreach (XmlElement node in nodes) { string strID = node.GetAttribute("id"); if (string.IsNullOrEmpty(strID) == false) { existing_ids.Add(strID); } } } // root.AppendChild(fragment); { // 一个一个加入,丢掉重复 id 属性值得 overdue 元素 XmlNodeList nodes = fragment.SelectNodes("overdue"); foreach (XmlElement node in nodes) { string strID = node.GetAttribute("id"); if (existing_ids.IndexOf(strID) != -1) { continue; } root.AppendChild(node); } } } if (itemdom != null) { // *** 检查册记录操作前在借的读者,是否指向另外一个读者。如果是这样,则需要事先消除相关的另一个读者记录的痕迹,也就是说相当于把相关的册给进行还书操作 string strBorrower0 = DomUtil.GetElementInnerText(itemdom.DocumentElement, "borrower"); if (string.IsNullOrEmpty(strBorrower0) == false && strBorrower0 != strReaderBarcode) { #if NO string strRemovedInfo = ""; // 去除读者记录侧的借阅信息链条 // return: // -1 出错 // 0 没有必要修复 // 1 修复成功 nRet = RemoveReaderSideLink( // Channels, channel, strBorrower0, strItemBarcodeParam, out strRemovedInfo, out strError); if (nRet == -1) { this.WriteErrorLog("册条码号为 '" + strItemBarcodeParam + "' 的册记录,在进行还书操作(拟被读者 '" + strReaderBarcode + "' 借阅)以前,发现它被另一读者 '" + strBorrower0 + "' 持有,软件尝试自动修正(删除)此读者记录的半侧借阅信息链。不过,在去除读者记录册借阅链时发生错误: " + strError); } else { this.WriteErrorLog("册条码号为 '" + strItemBarcodeParam + "' 的册记录,在进行还书操作(拟被读者 '" + strReaderBarcode + "' 借阅)以前,发现它被另一读者 '" + strBorrower0 + "' 持有,软件已经自动修正(删除)了此读者记录的半侧借阅信息链。被移走的片断 XML 信息为 '" + strRemovedInfo + "'"); } #endif } // *** 修改册记录 XmlElement nodeHistoryBorrower = null; string strBorrower = DomUtil.GetElementText(itemdom.DocumentElement, "borrower"); XmlNode dup_item_history = null; // 看看相同借者、借阅日期、换回日期的 BorrowHistory/borrower 元素是否已经存在 { string strBorrowDate = DomUtil.GetElementText(itemdom.DocumentElement, "borrowDate"); dup_item_history = itemdom.DocumentElement.SelectSingleNode("borrowHistory/borrower[@barcode='" + strBorrower + "' and @borrowDate='" + strBorrowDate + "' and @returnDate='" + strOperTime + "']"); } if (dup_item_history != null) { // 历史信息节点已经存在,就不必加入了 // 清空相关元素 DomUtil.DeleteElement(itemdom.DocumentElement, "borrower"); DomUtil.DeleteElement(itemdom.DocumentElement, "borrowDate"); DomUtil.DeleteElement(itemdom.DocumentElement, "returningDate"); DomUtil.DeleteElement(itemdom.DocumentElement, "borrowPeriod"); DomUtil.DeleteElement(itemdom.DocumentElement, "operator"); DomUtil.DeleteElement(itemdom.DocumentElement, "no"); DomUtil.DeleteElement(itemdom.DocumentElement, "renewComment"); } else { // 加入历史信息节点 // TODO: 也可从 domLog 中取得信息,创建 borrowHistory 下级事项。但要防范重复加入的情况 // 这里判断册记录中 borrower 元素是否为空的做法,具有可以避免重复加入 borrowHistory 下级事项的优点 if (string.IsNullOrEmpty(strBorrower) == false) { // 加入到借阅历史字段中 { // 看看根下面是否有borrowHistory元素 XmlNode root = itemdom.DocumentElement.SelectSingleNode("borrowHistory"); if (root == null) { root = itemdom.CreateElement("borrowHistory"); itemdom.DocumentElement.AppendChild(root); } nodeHistoryBorrower = itemdom.CreateElement("borrower"); // 插入到最前面 nodeHistoryBorrower = DomUtil.InsertFirstChild(root, nodeHistoryBorrower) as XmlElement; // 2015/1/12 增加等号左边的部分 // 如果超过100个,则删除多余的 while (root.ChildNodes.Count > 100) { root.RemoveChild(root.ChildNodes[root.ChildNodes.Count - 1]); } } #if NO DomUtil.SetAttr(nodeOldBorrower, "barcode", DomUtil.GetElementText(itemdom.DocumentElement, "borrower")); DomUtil.SetElementText(itemdom.DocumentElement, "borrower", ""); #endif SetAttribute(ref itemdom, "borrower", nodeHistoryBorrower, "barcode", true); #if NO DomUtil.SetAttr(nodeOldBorrower, "borrowDate", DomUtil.GetElementText(itemdom.DocumentElement, "borrowDate")); DomUtil.SetElementText(itemdom.DocumentElement, "borrowDate", ""); #endif SetAttribute(ref itemdom, "borrowDate", nodeHistoryBorrower, "borrowDate", true); #if NO DomUtil.SetAttr(nodeOldBorrower, "returningDate", DomUtil.GetElementText(itemdom.DocumentElement, "returningDate")); DomUtil.SetElementText(itemdom.DocumentElement, "returningDate", ""); #endif SetAttribute(ref itemdom, "returningDate", nodeHistoryBorrower, "returningDate", true); SetAttribute(ref itemdom, "borrowPeriod", nodeHistoryBorrower, "borrowPeriod", true); // borrowOperator SetAttribute(ref itemdom, "operator", nodeHistoryBorrower, "borrowOperator", true); // operator 本次还书的操作者 DomUtil.SetAttr(nodeHistoryBorrower, "operator", strReturnOperator); DomUtil.SetAttr(nodeHistoryBorrower, "returnDate", strOperTime); // TODO: 0 需要省略 SetAttribute(ref itemdom, "no", nodeHistoryBorrower, "no", true); // renewComment SetAttribute(ref itemdom, "renewComment", nodeHistoryBorrower, "renewComment", true); { string strText = strRecoverComment; string strOldRecoverComment = DomUtil.GetElementText(itemdom.DocumentElement, "recoverComment"); if (String.IsNullOrEmpty(strOldRecoverComment) == false) { strText = "(借阅时原注: " + strOldRecoverComment + ") " + strRecoverComment; } if (String.IsNullOrEmpty(strText) == false) { DomUtil.SetAttr(nodeHistoryBorrower, "recoverComment", strText); } } } if (strAction == "lost") { // 修改册记录的<state> string strState = DomUtil.GetElementText(itemdom.DocumentElement, "state"); if (nodeHistoryBorrower != null) { DomUtil.SetAttr(nodeHistoryBorrower, "state", strState); } if (String.IsNullOrEmpty(strState) == false) { strState += ","; } strState += "丢失"; DomUtil.SetElementText(itemdom.DocumentElement, "state", strState); // 将日志记录中的<lostComment>内容追加写入册记录的<comment>中 string strLostComment = DomUtil.GetElementText(domLog.DocumentElement, "lostComment"); if (strLostComment != "") { string strComment = DomUtil.GetElementText(itemdom.DocumentElement, "comment"); if (nodeHistoryBorrower != null) { DomUtil.SetAttr(nodeHistoryBorrower, "comment", strComment); } if (String.IsNullOrEmpty(strComment) == false) { strComment += "\r\n"; } strComment += strLostComment; DomUtil.SetElementText(itemdom.DocumentElement, "comment", strComment); } } } } return(0); }
private static string[] ExpandIncludes(XmlNode section, string baseDirectory, List <string> parentFileList) { if (!String.IsNullOrEmpty(baseDirectory) && !parentFileList.Contains(baseDirectory) && !String.IsNullOrEmpty(Path.GetFileName(baseDirectory))) { parentFileList.Add(baseDirectory); } if (!String.IsNullOrEmpty(baseDirectory)) { baseDirectory = Path.GetDirectoryName(baseDirectory); } List <string> includeFileList = new List <string>(); XmlNodeList includeNodeList = section.SelectNodes(String.Format("//include[@{0}]", PathToken)); foreach (XmlElement element in includeNodeList) { string includeFileName = element.GetAttribute(PathToken); //if (String.IsNullOrEmpty(includeFileName)) continue; if (!String.IsNullOrEmpty(baseDirectory) && !Path.IsPathRooted(includeFileName)) { includeFileName = Path.Combine(baseDirectory, includeFileName); } if (!String.IsNullOrEmpty(baseDirectory) && IsCircularFileExists(parentFileList, includeFileName)) { throw new ChoXmlDocumentException(String.Format("Circular reference encountered on the {0} file.", baseDirectory)); } if (!includeFileList.Contains(includeFileName)) { includeFileList.Add(includeFileName); } XmlDocument ownerDocment = section is XmlDocument ? section as XmlDocument : section.OwnerDocument; XmlDocumentFragment fragment = ownerDocment.CreateDocumentFragment(); try { XmlDocument includeDoc = ChoXmlDocument.Load(includeFileName); foreach (XmlAttribute attr in element.Attributes) { if (attr.Name != PathToken) { includeDoc.DocumentElement.SetAttribute(attr.Name, attr.Value); } } fragment.InnerXml = includeDoc.InnerXml; XmlElement xmlSubElement = (XmlElement)fragment.SelectSingleNode("/*"); string[] innerIncludeFileList = ExpandIncludes(xmlSubElement, baseDirectory, parentFileList); foreach (string innerIncludeFile in innerIncludeFileList) { if (!includeFileList.Contains(includeFileName)) { includeFileList.Add(innerIncludeFile); } } element.ParentNode.ReplaceChild(xmlSubElement, element); xmlSubElement.ParentNode.InsertBefore(xmlSubElement.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - BEGIN INCLUDE {0}", includeFileName))), xmlSubElement); xmlSubElement.ParentNode.AppendChild(xmlSubElement.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - END INCLUDE {0}", includeFileName)))); } catch (XmlException) { try { using (StreamReader sr = File.OpenText(includeFileName)) fragment.InnerXml = sr.ReadToEnd(); XmlNode parentNode = element.ParentNode; parentNode.RemoveChild(element); bool isFirstChild = true; XmlNode lastChild = null; foreach (XmlNode xmlNode in fragment.SelectNodes("/*|//comment()")) { if (xmlNode == null) { continue; } if (xmlNode is XmlElement && xmlNode.Name == "include") { string[] innerIncludeFileList = ExpandIncludes(xmlNode, includeFileName, parentFileList); //, false); foreach (string innerIncludeFile in innerIncludeFileList) { if (!includeFileList.Contains(includeFileName)) { includeFileList.Add(innerIncludeFile); } } } parentNode.AppendChild(xmlNode); if (isFirstChild) { parentNode.InsertBefore(parentNode.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - BEGIN INCLUDE {0}", includeFileName))), xmlNode); isFirstChild = false; } lastChild = xmlNode; } parentNode.InsertAfter(parentNode.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - END INCLUDE {0}", includeFileName))), lastChild); } catch (Exception innerEx) { throw new ChoXmlDocumentException(String.Format("{0}: Error loading xml file.", includeFileName), innerEx); } } catch (Exception ex) { throw new ChoXmlDocumentException(String.Format("{0}: Error loading xml file.", includeFileName), ex); } } return(includeFileList.ToArray()); }