Tag: xml namespaces

自动创建的用于xml反序列化的C#类不起作用

我正在努力为这个xml创建反序列化类: John Johnos Some Street 1 24 success 我试图使用自动创建的代码(在VisualStudio 12中:编辑 – >选择性粘贴 – >将XML粘贴为类): /// [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = “http://schemas.xmlsoap.org/soap/envelope/”)] [System.Xml.Serialization.XmlRootAttribute(Namespace = “http://schemas.xmlsoap.org/soap/envelope/”, IsNullable = false)] public partial class Envelope { private EnvelopeBody bodyField; private string encodingStyleField; /// public EnvelopeBody Body { get { return this.bodyField; } set { this.bodyField = value; […]

在同一个start元素标记中,前缀“无法从”重新定义为

我正在尝试使用C#生成以下xml元素。 我遇到的问题是我得到了exception:前缀“无法从”重新定义到同一个start元素标记内。 这是我的c#代码: XNamespace xsi = “http://www.w3.org/2001/XMLScema-instance” XElement foo = new XElement(“Foo”, new XAttribute(“xmlns”, “http://schemas.foo.com”), new XAttribute(Xnamespace.Xmlns + “xsi”, “http://www.w3.org/2001/XMLSchema-instance”), new XAttribute(xsi + “schemaLocation”, “http://schemas.foo.com http://schemas.foo.com/Current/xsd/Foo.xsd”)); 我怎样才能解决这个问题? 我正在尝试将生成的xml作为SOAP消息的主体发送,我需要它以接收器的这种格式。 编辑:我在另一个问题上找到了答案。 控制XML名称空间的顺序

什么导致Web服务URL和命名空间之间的差异?

我有一个包含Web服务的ASP.NET Web项目。 当我运行该服务时,它会使用类似于http://api.example.com/game/service.asmx的URL将我带到显示所有公开方法的页面。 在Web Service的代码中,有些方法具有以下属性: [WebService(Namespace = “http://webservices.example.com/GameServices/Game1”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Game1 : System.Web.Services.WebService { // code } 我对为什么具有webService属性的类上的命名空间与Web服务的路径不同有点困惑。 该命名空间来自哪里? 它刚刚组成了吗?

命名空间中不存在c#WPF AlternateContent

我只需要在设计时使用XAML代码。 我找到了一个很好的解决方案,可以在这里找到。 似乎有一些人在这里解决了XmlnsDefinitionAttribute的解析时间问题。 在我的情况下,问题是,我无法编译我的代码,因为在命名空间xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″找不到AlternateContent xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ 。 我确实没有找到任何关于这个命名空间的文档,而且看起来很奇怪, mc:Ignorable=”d”这一行并没有失败,这意味着我至少有一个包含上述命名空间的程序集。 这是我的代码: #if DEBUG [assembly: XmlnsDefinition(“debug-mode”, “MyNamespace”)] #endif … … 我的猜测是,我缺少一个Assembly-Reference,但是我还没有找到一个指定包含AlternateContent程序集的文档。 你有什么想法我可以让它工作吗? 编辑: 看起来这是一个常见的VS问题,可以用这个mc:Ignorable=”d mc”来解决mc:Ignorable=”d mc” 。 这在我的情况下不起作用,因为我需要在Design-Time中包含资源,VS-Designer应该可以使用它们:)

使用命名空间C#解析XML

我一直在努力解析这个XML数据。 看起来我的问题是命名空间。 我认为我的上一个网站http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl有一丝希望,但它也失败了。 每次我运行下面的代码时,我都会得到“对象引用未设置为对象的实例”。 似乎有很多方法用C#解析XML,但这些命名空间让我感到害怕。 我究竟做错了什么? 请记住,我是XML和C#的新手,并且用于解析JSON数据。 是的,我用Google搜索了几种解析XML数据的方法,在这种情况下没有任何用处。 我可以在没有命名空间的情况下获取XML,但是它们始终存在错误。 C#代码: XElement element = XElement.Parse(xml); XNamespace ns2 = “http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd”; string val = element.Element(ns2 + “ItemDimensions”).Element(ns2 + “Heigth”).Value; Console.WriteLine(val); XML数据: Troelsen, Andrew Paperback Apress 5 9.21 7.48 2.52 5.80 false true false Apress english Unknown english Original Language english Published 59.99 USD Apress 1 1752 2.60 9.20 […]

如何使用linq xml将命名空间添加到xml

问题更新:如果我的问题不明确,我很抱歉 这是现在使用的代码 XDocument doc = XDocument.Parse(framedoc.ToString()); foreach (var node in doc.Descendants(“document”).ToList()) { XNamespace ns = “xsi”; node.SetAttributeValue(ns + “schema”, “”); node.Name = “alto”; } 这是输出 我的目标是这样的 xsi:schemaLocation=”” p1和xmlns:p1=”xsi”来自哪里?

使用命名空间别名而不是XElement上的URI选择命名空间的XML节点属性

我正在尝试从严格命名的XML文档中查询一些信息,并且在查找也是命名空间的属性时遇到了一些麻烦。 XML看起来像: … 我的目标是创建一个包含国家/地区代码和国家/地区名称的对象列表。 这对我现在有用: XmlReader reader = XmlReader.Create(@”path/to/xml.xml”); XDocument root = XDocument.Load(reader); XmlNameTable nameTable = reader.NameTable; XmlNamespaceManager nsManager = new XmlNamespaceManager(nameTable); nsManager.AddNamespace(“rdf”, “http://www.w3.org/1999/02/22-rdf-syntax-ns#”); nsManager.AddNamespace(“rdfs”, “http://www.w3.org/2000/01/rdf-schema#”); nsManager.AddNamespace(“skos”, “http://www.w3.org/2004/02/skos/core#”); nsManager.AddNamespace(“geo”, “http://www.geonames.org/ontology#”); var geoCountries = from country in root.XPathSelectElements(“./rdf:RDF/geo:Country”, nsManager) select new { CountryCode = country.Attributes(“{http://www.w3.org/2004/02/skos/core#}notation”).First().Value, CountryName = country.Attributes(“{http://www.w3.org/2000/01/rdf-schema#}label”).First().Value }; 这工作正常,但我想使用命名空间别名找到属性,而不是命名空间URI(只是因为),或者至少能够使用别名查找URI。 为了尝试后一种想法,我最终想通了我可以做到这一点: country.Attributes(nsManager.LookupNamespace(“skos”) + “notation”).First().Value 但是我得到一个XmlException :’:’字符,hex值0x3A,不能包含在名称中。 […]

找不到类型或命名空间名称(XAML新增function)

所以我正在尝试使用Visual Studio 2012在XAML文档上使用一个非常简单的骰子应用程序,但我无法弄清楚如何处理错误。 我尝试使用bitmapimage添加; 但它仍然行不通。 以下是我得到的错误: Error 1 Undefined namespace. The ‘using’ URI refers to a namespace ‘DiceRoll’ that could not be found. Error 2 The type or namespace name ‘BitmapImage’ could not be found (are you missing a using directive or an assembly reference?) CS: using System; using System.Collections.Generic; using System.IO; using System.Linq; using […]

ASP.NET Web API中的XML命名空间

我目前正在开发一个项目,要求我从端点和JSON输出XML。 我有以下型号: [DataContract(Namespace=”http://www.yale.edu/tp/cas”)] [XmlType(“serviceResponse”)] [XmlRoot(Namespace=”http://www.yale.edu/tp/cas”)] public class ServiceResponse { [XmlElement(“authenticationSuccess”)] public AuthenticationSuccess Success { get; set; } [XmlElement(“authenticationFailure”)] public AuthenticationFailure Failure { get; set; } } 当success不为null时,输出如下: 现在,我可以看到,显然,我没有为命名空间分配前缀,我告诉元素是其中的一部分。 我的问题是我找不到使用媒体格式化程序在MVC4中添加名称空间前缀的地方。 我在global.asax中有以下内容: GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; GlobalConfiguration.Configuration.Formatters.XmlFormatter.RemoveSerializer(typeof(Models.ServiceResponse)); GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(typeof(Models.ServiceResponse), new Infrastructure.NamespaceXmlSerializer(typeof(Models.ServiceResponse))); 我创建了一个基于XmlSerializer的自定义序列化程序,试图拦截写入请求并在那里添加命名空间列表。 这个方法的问题是,现在我在每个可重写方法中都有断点,并且在序列化时没有一个断点导致我相信我的序列化器没有被使用。 是否有一些内置的方法来完成我想要做的事情,或者我是否在重新实现XmlMediaTypeFormatter以在序列化对象时传入命名空间?

加载XDocument时是否可以使用预定义的命名空间?

我经常需要处理包含命名空间元素的XML文档,但不会声明命名空间。 例如: 由于前缀“a”从未分配名称空间URI,因此该文档无效。 当我使用以下代码加载这样的XML文档时: using (StreamReader reader = new StreamReader(new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { doc = XDocument.Load(reader, LoadOptions.PreserveWhitespace); } 它抛出一个exception,说明(正确地)该文档包含一个未声明的命名空间,并且格式不正确。 那么,我可以预先定义默认名称空间前缀 – >名称空间URI对,以便解析器可以依赖吗? XMLNamespaceManager看起来很有前途,但不知道如何将它应用于这种情况(或者如果我可以)。