Tag: xml

如何按页码访问OpenXML内容?

使用OpenXML,我可以按页码阅读文档内容吗? wordDocument.MainDocumentPart.Document.Body提供完整文档的内容。 public void OpenWordprocessingDocumentReadonly() { string filepath = @”C:\…\test.docx”; // Open a WordprocessingDocument based on a filepath. using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filepath, false)) { // Assign a reference to the existing document body. Body body = wordDocument.MainDocumentPart.Document.Body; int pageCount = 0; if (wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text != null) { pageCount = Convert.ToInt32(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text); } for (int i = […]

HRESULT:0xC00CE556 – 将字符串加载到XML

我正在尝试加载包含从SkyDrive下载的XML的字符串。 XmlDocument myXML = new XmlDocument(); myXML.LoadXml(importXMLDocument); 当我调用上面的代码时,我收到以下错误: 来自HRESULT的exception:0xC00CE556 这是我试图从字符串和加载转换为XML文档的XML: Tahoe 2004 97742 5w-30 6 0 0 0 0 0 97742 2012-05-04T19:53:53.358-06:00 2012-08-04T19:53:53.358-06:00 100742 false false 3000 3 true true true 2 Mazda3 2011 21504 abcdefg 0w-20 0 0 0 0 0 0 21504 2012-09-14T18:05:02.298-06:00 2013-02-14T18:05:02.298-07:00 26504 false false 64.75 need new tires – $500+ […]

如何序列化List <List >?

框架是c#.net 4.6.2 我正在从XML代码生成自动XML类 当我自动生成时,它会自动转换为Array[][] 但我想用它作为List<List> 我确信我从Array到List的对话会导致一些序列化错误。 我认为这是关于获取和设置function。 所以我需要你的帮助来解决这个问题 这里是我编辑>粘贴特殊>粘贴XML作为类时自动生成的代码片段 /// [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class OxFordDefinition_perGroup { private string _GroupDescField; private string _GroupSenseField; private string _GroupGrammerField; private OxFordDefinition_perGroup_perMainExample_perSubExample[][] _perMainExampleField; /// public string _GroupDesc { get { return this._GroupDescField; } set { this._GroupDescField = value; } } /// public string _GroupSense { get […]

将XML文档合并到空XML节点中?

编辑 :这与链接的发布不完全相同。 我遇到的主要问题是将子节点附加到空XML节点。 直接选择节点时,它将返回System.String类型,该类型没有AppendChild方法。 修复是选择所有子节点,然后根据Dan的建议按名称过滤它。 $emptyNode= $root.ChildNodes | ? { $_.name -eq “customers” } 我主要使用的是powershell,但是我正在使用的很多代码下面都使用的是.NET System对象。 我想要做的事情可能更好地通过一个例子来解释。 假设我有三个XML文档: ACME Co Alex 20 Aaron 21 Allison 22 和 Big Co Bob 30 Barry 31 Bill 32 和 Cool Co Carl 40 Craig 41 Chris 42 我有一个“根”文档,看起来像: 我想在根文档下合并三个A,B和C文档,这样我的最终产品将是: ACME Alex 20 Aaron 21 Allison 22 Big Co […]

“非静态字段,方法或属性需要对象引用”

我在我的代码中遇到了这个小问题。 我正在尝试制作可以写入xml文档的小型控制台应用程序。 我使用过xmldocument和xmlnode概念。 我得到的错误是; *非静态字段,方法或属性’Write_xml.Program.give_node(System.Xml.XmlDocument)’C:\ Documents and Settings \ Administrator \ Desktop \ Write_xml \ Write_xml \ Program.cs *需要对象引用 代码没问题,除了1错误。 我无法解决它,我希望有人检查并纠正它。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace Write_xml { class Program { static void Main(string[] args) { XmlDocument doc = new XmlDocument(); XmlDocument lets = new XmlDocument(); string […]

将XPath与XML命名空间一起使用

我想用XPath处理下面的xml: Tere是根元素的xmlns 。 我的代码是这样的: XElement doc = XElement.Load(xmlFilePath); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace(“prefix”, “http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration”); XElement settingDiagConnectionString = doc.XPathSelectElement(“//prefix:ServiceConfiguration/Role/ConfigurationSettings/Setting[1]”, ns); // <===== always return null. settingDiagConnectionString.SetAttributeValue("value", "hello, world!"); 但是settingDiagConnectionString始终为null。 为什么? 加1 谢谢har07。 为每个元素添加前缀后,以下代码有效: XmlDocument doc = new XmlDocument(); doc.Load(cscfgPath); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace(“prefix”, “http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration”); XmlNode settingDiagConnectionString = doc.SelectNodes(“/prefix:ServiceConfiguration/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]”, ns)[0]; settingDiagConnectionString.Attributes[“value”].Value = […]

我可以创建一个带正斜杠的元素作为名称的一部分

我可以创建一个带正斜杠的元素作为名称的一部分吗? fruit 对我来说似乎无效但只是想确认一下。

‘xsi’是使用XmlDocument的未声明的前缀

我收到’xsi’是使用XmlDocument的未声明的前缀。 我正在尝试读取具有以下架构的文件: test 我尝试过以下方法: XmlDocument xmldoc = new XmlDocument(); using (XmlTextReader tr = new XmlTextReader(strXmlFile)) { //tr.Namespaces = false; (uncomment to ignore namespace) xmldoc.Load(tr); // ‘xsi’ is an undeclared prefix error here } 如果我取消注释该行以忽略命名空间,它会加载正常但后来无法保存XmlDocument 。 所以忽略它不会是一个解决方案。 有谁知道如何正确加载架构? 问题/错误似乎出现在此节点中: 更新#1我尝试了以下内容: XmlDocument xmldoc = new XmlDocument(); NameTable nt = new NameTable(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); nsmgr.AddNamespace(“xsi”, […]

附加现有XML文件

我有一个现有的XML文件,我想在不改变格式的情况下追加。 现有文件如下所示: 1 Steve Lawrence NYC 如何使用此格式添加其他用户? 我现有的代码是: string fileLocation = “clients.xml”; XmlTextWriter writer; if (!File.Exists(fileLocation)) { writer = new XmlTextWriter(fileLocation, null); writer.WriteStartDocument(); // Write the Root Element writer.WriteStartElement(“Clients”); // End Element and Close writer.WriteEndElement(); writer.Close(); } // Append new data here 我曾想过使用XmlDocument Fragment来附加数据,但我不确定我是否可以维护现有格式(和空标签)而不会弄乱文件。 您可以给予的任何建议非常感谢。 编辑我已经更改了代码以读取原始XML但文件仍然被覆盖。

仅在一个对象时将XML转换为Json数组

我目前正在使用Newtonsoft将一些xml转换为json以从RestExtension返回。 我的xml是以。的forms name detail name detail 我把它转换为json使用 JsonConvert.SerializeXmlNode(xmldocument); 如果有多个项目,这可以正常工作。 我得到了这个 – json中的一个项目数组(这是我需要的): {“Items”:{“Item”:[{“Name”:”name”,”Detail”:”detail”},{“Name”:”name”,”Detail”:”detail”}]}} 但是当只有一个时,可以理解的是这样转换(不是数组): {“Items”:{“Item”:{“Name”:”name”,”Detail”:”detail”}}} 我正在阅读此内容的应用程序开发人员需要json返回一个项目数组,无论是否有一个或多个。 有没有办法欺骗它认为它是一个数组或者有人建议另一种方式这样做?