Tag: xml

当缩进xml时,XMLREADER会给出不同的结果

我在两个不同的文件中有相同的XML。 在一个文件中,XML缩进 – 另一个不缩进。 XML如下: 使用以下代码时,我得到两个文件的不同结果: XmlReaderSettings settings = new XmlReaderSettings { IgnoreComments = true, IgnoreWhitespace = false, IgnoreProcessingInstructions = true }; using (XmlReader reader = XmlReader.Create(invoiceStream, settings)) { reader.MoveToContent(); reader.Read(); var prevLocalname = reader.LocalName; var element = XNode.ReadFrom(reader) as XElement; var newLocalname = reader.LocalName; } 使用缩进文件,我得到以下值: prevLocalname = “”; newLocalname = “element1”; 如果文件没有缩进,我会得到以下值: prevLocalname […]

基于具有“ID”和“父ID”的元素序列重构XML树

我想要发生的是我想要将ID等于ParentID的元素放入其中? 所以在我的例子中,ParentId = 1的组应该在Id = 1的组中,我怎么能这样做? 好困惑.. 截至目前,这是我的代码: XElement xroot = new XElement(“Root”); XElement xnavigations = null; XElement xmenus = null; foreach (DataRow navigations in GetNavigationSets().Rows) { xnavigations = new XElement(“Group”, new XElement(“GroupName”, navigations[“name”].ToString()) ); xnavigations.SetAttributeValue(“Id”, navigations[“id”].ToString()); xnavigations.SetAttributeValue(“ParentId”, navigations[“parent_id”].ToString()); foreach (DataRow menus in GetMenusInNavigationSetByNavigation(int.Parse(navigations[“id”].ToString())).Rows) { foreach (DataRow menu in GetMenuById(int.Parse(menus[“menu_id”].ToString())).Rows) { xmenus = new XElement(“Menu”, […]

XmlSerializer:删除xmlns:xsd但保留xmlns:xsi

我正在使用此方法生成XML: using (MemoryStream msRes = new MemoryStream()) using (StreamWriter objStreamWriter = new StreamWriter(msRes)) using (XmlWriter xw = XmlWriter.Create(objStreamWriter, new XmlWriterSettings() { Indent = true, IndentChars = String.Empty })) { XmlSerializer serializer = new XmlSerializer(doc.GetType()); serializer.Serialize(xw, doc); return msRes.ToArray(); } 结果是我有这样的行 。 我想删除属性xmlns:xsd=”http://www.w3.org/2001/XMLSchema”但保留xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”urn:iso:std:iso:20022:tech:xsd:camt.054.001.02″ 。 我怎样才能做到这一点 ? 谢谢你的帮助 !

按特定顺序将并行数组序列化为C#中的XML

我目前的任务是将数据发送到具有指定列表的奇怪方式的Web服务。 我无法控制架构,我尝试让另一方更改架构失败了。 所以我几乎坚持这一点。 它们的模式定义方式是这样的(仅包含相关位): 我使用xsd.exe生成一个C#类来轻松地序列化结构。 生成的位如下: [System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “4.0.30319.33440”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=”the namespace”)] public partial class PartList { [System.Xml.Serialization.XmlElementAttribute(“Name”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] Name { get; set; } [System.Xml.Serialization.XmlElementAttribute(“Data”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] Data { get; set; } [System.Xml.Serialization.XmlElementAttribute(“OtherData”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] OtherData { get; set; } } 是的,并行数组。 现在,根据他们的文档(以及来自通过其他方式生成XML的另一方的轶事数据),正确/预期的xml应该如下所示(例如,包含两个项目的列表 – 为了说明目的添加注释内联): Some Test 123131313 0.11 […]

使用C#将列表 xml文件的属性序列化和反序列化

如何使用C#序列化和反序列化xml文件。 我为这个xml创建了可序列化的类。 在下面的一些代码中反序列化这个xml,列表只能得到单个值。 ff gg jj [Serializable, XmlRoot(“Configuration”), XmlType(“Configuration”)] public class Configuration { public Configuration() { CSVFile = new List(); } [XmlElement(“CSVFile”)] public List CSVFile { get; set; } } public class Mytutorial { string configFilePath = “above xml file path” XmlSerializer serializer = new XmlSerializer(typeof(Configuration)); FileStream xmlFile = new FileStream(configFilePath, FileMode.Open); Configuration con = […]

C#使用XSD文件validationXML文件

我有一个XML文件 – 它是从解析器生成的, 我有一个XSD文件 。 关键是使用XSD文件(来自特定路径)validationXML文件(来自特定路径),如果已validation,则返回标志。 我看到的大部分代码都没有使用XSD文件来validation。 有没有办法使用XSD 文件validationXML 文件 ?

解压缩文件错误

我正在使用www.icsharpcode.net上的SharpZipLib开源.net库 我的目标是解压缩xml文件并将其读入数据集。 但是,在将文件读入数据集时出现以下错误:“根级别的数据无效。第1行,位置1”。 我相信正在发生的事情是解压缩代码没有释放文件,原因如下。 1.)如果我解压缩文件并退出应用程序。 当我重新启动应用程序时,我可以将解压缩的文件读入数据集。 2.)如果我在写完之后立即读入xml文件(没有压缩),那么它工作正常。 3.)如果我将数据集写入xml,将其压缩,解压缩,然后尝试读取它,我得到exception。 下面的代码很简单。 UnZipFile将返回刚解压缩的文件名。 在此调用下方是调用将其读入数据集。 变量fileToRead是新解压缩的xml文件的完整路径。 string fileToRead = UnZipFile(filepath, DOViewerUploadStoreArea); ds.ReadXml(fileToRead ) private string UnZipFile(string file, string dirToUnzipTo) { string unzippedfile = “”; try { ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry myEntry; string tmpEntry = String.Empty; while ((myEntry = s.GetNextEntry()) != null) { string directoryName = dirToUnzipTo; string […]

发布使用XSD反序列化字符串

我在尝试使用XSD反序列化XML字符串时遇到了麻烦。 我正在使用c#/ Asp.Net和DataContractSerializer。 这是从第三方返回的字符串: 11625807 Declined 1 No Qualified Buyers Found 这是我正在尝试创建的类: [DataContract(Name = “ApplicationResponse”, Namespace = “”)] public class SCFResponse { [DataMember] public string Id { get; set; } [DataMember] public string Status { get; set; } [DataMember] public string Response { get; set; } [DataMember] public decimal Price { get; set; } [DataMember] […]

如何在不进行任何修改的情况下添加InnerXml?

我试图找到一种简单的方法来将XML添加到XML-with-xmlns而不必获取xmlns=””也不必每次都指定xmlns 。 我尝试了XDocument和XmlDocument但找不到简单的方法。 我得到的最接近的是这样做: XmlDocument xml = new XmlDocument(); XmlNode docNode = xml.CreateXmlDeclaration(“1.0”, “UTF-8”, null); xml.AppendChild(docNode); XmlElement root = xml.CreateElement(“root”, @”http://example.com”); xml.AppendChild(root); root.InnerXml = “b”; 但我得到的是: b 那么:有没有办法设置InnerXml而不进行修改?

通过c#不接受XML中的日期时间值

无法将datetime.now值传递给节点’createddatetime’。 输出xml文件丢弃该节点。 我使用以下代码, string PATH = “C:\\Samplex.xml”; CreateEmptyFile(PATH); var data = new AutoCount(); data.Product = “AutoCount Accounting”; data.Version = “1.5”; data.CreatedApplication = “BApp”; data.CreatedBy = “Business Solutions”; data.CreatedDateTime = DateTime.Now; /* this line*/ var serializer = new XmlSerializer(typeof(AutoCount)); using (var stream = new StreamWriter(PATH)) serializer.Serialize(stream, data); 而且输出是: AutoCount Accounting 1.5 BApp Business Solutions 代替 : […]