C# 从web服务C读取XML的技巧#
我到处找了,找不到任何帮助我的东西 我正在编写一个连接到Web服务的程序,然后Web服务发送一个XML响应。在收到响应后,我必须从中检索某些值,但这是需要技巧的地方 以下是返回的XML的一个片段:C# 从web服务C读取XML的技巧#,c#,xml,web-services,xpath,nodes,C#,Xml,Web Services,Xpath,Nodes,我到处找了,找不到任何帮助我的东西 我正在编写一个连接到Web服务的程序,然后Web服务发送一个XML响应。在收到响应后,我必须从中检索某些值,但这是需要技巧的地方 以下是返回的XML的一个片段: <?xml version="1.0"?> <MobilePortalSellingCategoriesHierarchy xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/20
<?xml version="1.0"?>
<MobilePortalSellingCategoriesHierarchy xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Response xmlns="http://blabla.com/service/">Successful</Response>
<ResponseNumber xmlns="http://blabla.com/service/">0</ResponseNumber>
<SellingCategoriesHierarchy xmlns="http://tym2sell.com/PortalService/">
<Response>Successful</Response>
<ResponseNumber>0</ResponseNumber>
<SellingCategories>
<PortalSellingCategory>
<SellingCategoryId xsi:nil="true" />
<SellingCategoryName>category1</SellingCategoryName>
<DeliveryMethod />
<DeliveryMethodNumber>0</DeliveryMethodNumber>
<SellingCategories>
<PortalSellingCategory>
<SellingCategoryId xsi:nil="true" />
<SellingCategoryName>category1_Item</SellingCategoryName>
<DeliveryMethod />
<DeliveryMethodNumber>0</DeliveryMethodNumber>
<SellingCategories>
<PortalSellingCategory>
<SellingCategoryId>2</SellingCategoryId>
<SellingCategoryName>Item2</SellingCategoryName>
<DeliveryMethod>Display</DeliveryMethod>
<DeliveryMethodNumber>1</DeliveryMethodNumber>
<VoucherValue>0.00</VoucherValue>
<IsVariablePrice>true</IsVariablePrice>
<MinimumVoucherValue>1.00</MinimumVoucherValue>
<MaximumVoucherValue>1000.00</MaximumVoucherValue>
<VoucherValueIncrement>1.00</VoucherValueIncrement>
<AdditionalInputItems>
<PortalAdditionalInputItem>
<InputItemId>-1</InputItemId>
<Label>Value:</Label>
<IsNumericOnly>true</IsNumericOnly>
<MaximumLength>7</MaximumLength>
<Hidden>false</Hidden>
</PortalAdditionalInputItem>
<PortalAdditionalInputItem>
<InputItemId>4</InputItemId>
<Label>Mobile Number</Label>
<IsNumericOnly>true</IsNumericOnly>
<MaximumLength>15</MaximumLength>
<Hidden>false</Hidden>
</PortalAdditionalInputItem>
</AdditionalInputItems>
<TwoStep>false</TwoStep>
<SelectedIcon>SamplePicture</SelectedIcon>
<UnSelectedIcon>SamplePicture</UnSelectedIcon>
我已尝试更改xml.SelectNodes(“/MobilePortalSellingCategoriesHierarchy”)代码>
到
xml.SelectNodes(“/MobilePortalSellingCategories层次结构/SellingCategories层次结构/SellingCategories层次结构/SellingCategories/PortalSellingCategories层次结构”)代码>
我需要选择每个SellingCategoryName并列出SellingCategoryName及其下的所有其他项目
我希望在以下方面有所收获:
类别1
类别1_项目
项目2
样本图片
样图
Mine只读取第一个节点,然后将“Successful”返回给我
我也试过:
XElement root = XElement.Load("FilePath");
var sellingCategoryNames = from PortalSellingCategory in root.Elements("MobilePortalSellingCategoriesHierarchy")
where (string)PortalSellingCategory.Element("SellingCategoriesHierarchy").Element("SellingCategories").Element("PortalSellingCategory") != ""
select PortalSellingCategory;
foreach (var xEle in sellingCategoryNames)
{
richTextBox1.Text = (string)xEle;
}
任何帮助都将不胜感激。您正在做什么
xml.SelectNodes("/MobilePortalSellingCategoriesHierarchy");
正在选择根节点,这只是一个。这就是为什么你的单子上只有一个项目被退回的原因。等级制度重要吗?我可以看到PortalSellingCategory也可以位于另一个PortalSellingCategory中。如果没有,您可以尝试以下选择:
xml.SelectNodes("//PortalSellingCategory");
这将在您的响应中搜索每个名为“PortalSellingCategory”的节点,而不搜索层次结构中的任何位置
编辑:
是的,你应该注意名称空间,sry没有看到。
如果您喜欢使用XPath设置所有节点,则必须创建一个新的NamespaceManager,并在其上调用selectNodes:
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("ns", "http://tym2sell.com/PortalService/");
XmlNodeList xnList = xml.SelectNodes("//ns:PortalSellingCategory", ns);
我将使用XElement而不是XMLDocument,然后使用Linq来查询或拾取ELElement,如:XElement xContact=
int contactno=(int?)xContact.Element(“命令”).Element(“联系人”).Attribute(“联系人ID”)-1;
if(xContact.Element(“command”).Element(“contactperson”).Element(“name”).Element(“firstname”)!=null)
console.writeline(xContact.Element(“command”).Element(“contactperson”).Element(“name”).Element(“firstname”).Value)
嗨
它返回节点集合,您只需查询它即可获得所需信息。
如果需要,请随时寻求帮助
Dimitri。我强烈建议在这里使用LINQ to XML。我怀疑是名称空间一开始就把你搞砸了,而LINQ to XML使名称空间变得更容易。接下来,您似乎要在MobilePortalSellingCategoryName
层次结构中直接查找SellingCategoryName
,但实际上涉及到几个层……我怀疑OP也需要指定名称空间。。。所有这些都可以在LINQtoXML中轻松完成,不需要XPath。“枚举没有结果”编辑了我的答案。是的,Linq可能会更好,但我认为至少用这种方法来检验它的工作原理是好的…如果它之后很糟糕,你仍然可以换成Linq。但归根结底,您仍然知道解决此问题的不止一种方法。如果层次结构很重要,我将如何工作?您可以使用各种XPath语句,如“//PortalSellingCategory//PortalSellingCategory”,来获取嵌入到另一个PortalSellingCategory中任何位置的每个PortalSellingCategory。但是要了解XPath的各个方面,可以查看类似的内容以获得完整的概述
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("ns", "http://tym2sell.com/PortalService/");
XmlNodeList xnList = xml.SelectNodes("//ns:PortalSellingCategory", ns);
var doc= new XmlDocument();
doc.Load("FilePath");
var nodeList = xml.GetElementsByTagName("PortalSellingCategory");