简体   繁体   English

C#Xdocument和具有相同名称的后代不起作用

[英]c# Xdocument and descendants with the same name not working

Hi have the following XML with the same Transaction name (can't change it because this is how it comes from the origin): 嗨,下面的XML具有相同的交易名称(无法更改,因为这是从源头来的):

<StoreCenter Operation="update" xmlns="http://something.com/rdc.xsd">
 <Transaction>
  <Transaction>
   <StoreID>30</StoreID>
   <TransactionID>2</TransactionID>
   <RegisterTransNumber>2</RegisterTransNumber>
    ....
  </Transaction>
  <Transaction>
   <StoreID>30</StoreID>
   <TransactionID>3</TransactionID>
   <RegisterTransNumber>2</RegisterTransNumber>
   ....
  <Transaction>
 <Transaction>
<StoreCenter>

I have the following code and I'm new in LINQ, I'm trying to retrive the StoreID for each Transaction Child: 我有以下代码,并且是LINQ的新手,我试图为每个Transaction Child检索StoreID:

XDocument Doc = XDocument.Load(filename);
       XNamespace ns = "http://something.com/rdc.xsd";
        foreach (var StoreCenter in Doc.Descendants(ns + "StoreCenter"))
        {
            foreach (var Transaction in StoreCenter.Descendants("Transaction"))
            {
                 foreach (var TransactionCh in Transaction.Descendants("Transaction"))
                 {
                    Console.WriteLine(Transaction.Element("StoreID").Value);
                 }

            }
        }

But I'm getting nothing, what am I doing wrong?, is this a good approach to retrieve these values?, please your advice will be appreciated 但是我什么也没收到,我在做什么错?这是检索这些值的好方法吗?请您的建议将不胜感激。

Remove the curly brace from here: 从此处删除花括号:

Doc.Descendants("{" + ns + "StoreCenter")
                ^^^

And you should specify the namespace with child elements as well.Like: StoreCenter.Descendants(ns + "Transaction") and Transaction.Descendants(ns + "Transaction") 并且您还应指定带有子元素的名称空间。例如: StoreCenter.Descendants(ns + "Transaction")Transaction.Descendants(ns + "Transaction")

if you just want to get Transaction elements you can just to : Doc.Descendants(ns + "Transaction") or Doc.Root.Element(ns + "Transaction").Elements(ns + "Transaction"); 如果您只想获取Transaction元素,则可以: Doc.Descendants(ns + "Transaction")Doc.Root.Element(ns + "Transaction").Elements(ns + "Transaction"); (assuming StoreCenter is the root element) (假设StoreCenter是根元素)

If your XML is correctly closed (i assume that the Transaction and StoreCenter nodes from the example XML are closed), then - yes, your approach using LINQ2XML is correct. 如果您的XML已正确关闭(我假设示例XML中的TransactionStoreCenter节点已关闭),则-是的,您使用LINQ2XML的方法是正确的。

You could enhance the code using the XElement.GetDefaultNamespace() method to retrieve the default namespace easily (easier than typing). 您可以使用XElement.GetDefaultNamespace()方法增强代码,以轻松检索默认名称空间(比键入更容易)。 A solution could look like this: 解决方案如下所示:

    var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<StoreCenter Operation=""update"" xmlns=""http://something.com/rdc.xsd"">
    <Transaction>
        <Transaction>
            <StoreID>30</StoreID>
            <TransactionID>2</TransactionID>
            <RegisterTransNumber>2</RegisterTransNumber>
        </Transaction>
        <Transaction>
            <StoreID>30</StoreID>
            <TransactionID>3</TransactionID>
            <RegisterTransNumber>2</RegisterTransNumber>
        </Transaction>
    </Transaction>
</StoreCenter>";

var xmlDocument = XDocument.Parse(xml); // or XDocument.Load(xml);
var ns = xmlDocument.Root.GetDefaultNamespace();
var transactions = xmlDocument
                    .Root                           // the StoreCenter root node
                    .Element(ns + "Transaction")    // the enclosing Transaction node
                    .Elements(ns + "Transaction")   // only Transaction subnodes
                    .ToList();
foreach (var transaction in transactions)
{
    var storeId = transaction.Element(ns + "StoreID").Value;
    Console.WriteLine(storeId);
} 

And the output is: 输出为:

30
30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM