簡體   English   中英

C#從特定元素中檢索多個XML屬性以進行操作

[英]C# retrieve multiple XML attributes from specific elements for manipulation

初學者C#人士在這里,我一直在努力尋求您的幫助。 我需要處理一個XML文件,每次閱讀它的內容都會有所不同。 每次閱讀該文檔時,都需要在特定元素內搜索特定屬性。 這些特定元素和屬性的組成可能因文件而異。 我使用在此論壇上找到的利用Linq到XML的示例成功讀取了單個元素和該元素中的單個屬性。 請參閱以下有關我正在使用的XML的示例。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
    <w:body>
        <w:p w14:paraId="2CBBB1B4" w14:textId="77777777" w:rsidR="00D9548A" w:rsidRDefault="00D9548A" w:rsidP="00ED7A0B"></w:p>
        <w:p w14:paraId="2CBBB1B5" w14:textId="77777777" w:rsidR="00ED548A" w:rsidRPr="00ED77B9" w:rsidRDefault="00C706DD" w:rsidP="00D9548A"></w:p>
        <w:pPr>
            <w:rPr>
                <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"></w:rFonts>
                <w:b></w:b>
                <w:sz w:val="40"></w:sz>
                <w:szCs w:val="40"></w:szCs>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="00cool6F"></w:r>
        <w:tr w:rsidR="0029258E" w14:paraId="2CBBB242" w14:textId="77777777" w:rsidTr="0029258E"></w:tr>
    </w:body>
</w:document>

有趣的元素是'w:p','w:r','w:tr'或幾乎任何具有以下任何屬性的元素:wrsidR,wrsidRDefault,wrsidRPr,rsidTr。

理想情況下,我想要一種將這些值中的每一個讀入某種list \\ array的方法,以便我可以更改這些值並將這些值寫回到從中獲取它們的屬性中。

我拼湊的代碼僅從文件中的“ w:p”元素返回1個屬性“ rsidR”。

public static void foobar()
    {
        string strFile = @"C:\SourceFolder\SampleXML\document-test.xml";
        XDocument xDoc = XDocument.Load(strFile);
        XNamespace xNsp = xDoc.Root.Name.Namespace;

        var values = from rsids in xDoc.Descendants(xNsp + "p").Attributes(xNsp + "rsidR")
                     select rsids.Value.ToString();

        foreach (var v in values)
        {
            Console.WriteLine(v);
        }
    }

跟蹤此文件中所有有趣的屬性值的最佳方法是什么,以便我可以遍歷它們,更改值並將其寫回到XML文件?

一如既往,感謝您的幫助!

創建一個(如果可能)或多個與您的屬性匹配的XPath。 選擇節點,更改值(如果值取決於匹配的元素,則必須在此處進行說明),保存回XML。

https://msdn.microsoft.com/zh-CN/library/d271ytdx%28v=vs.110%29.aspx

    After you load the document in XmlDocument available in using System.Xml 
namespace, you can create mutliple XmlNodeList(s) and then fetch the values.

         public static void foobar()
         {
            string strFile = @"C:\SourceFolder\SampleXML\document-test.xml";
            XmlDocument doc = XmlDocument.Load(strFile);

            if (doc.SelectSingleNode("w:body") != null)
            {
               XmlNodeList nodes = doc.SelectNodes(".//w:pPr");
               foreach (XmlNode xn in nodes)
               {
                   XmlNodeList rPr = xn.SelectNodes("w:rPr");
                   foreach (XmlNode xnrPr in rPr)
                   {
                      if (xnrPr.SelectSingleNode("w:rFonts") != null)
                      {
                          Console.WriteLine(xnrPr.SelectSingleNode("w:rFonts").InnerText.ToString());
                      }
                   }
               }
            }

這段丑陋的代碼似乎可以完成我想要的工作...我很樂意接受任何精簡的\\高效的執行相同操作的代碼。 要解決的突出問題是:

1)僅獲取屬性的值2)將屬性值聚合到一個數組中,以進行迭代。

public static void shaqfoo()
    {
        string strFile = @"C:\SourceFolder\SampleXML\document (3).xml";
        using (XmlReader reader = XmlReader.Create(strFile))
        {
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        case "w:p":

                            string wp_rsidRAttrib = reader["w:rsidR"];
                            string wp_rsidRDefaultAttrib = reader["w:rsidRDefault"];
                            string wp_rsidPAttrib = reader["w:rsidP"];
                            string wp_rsidRPrAttrib = reader["w:rsidRPr"];
                            string wp_rsidTrAttrib = reader["w:rsidTr"];

                            if (wp_rsidRAttrib != null)
                            {
                                Console.WriteLine("w:p : w:rsidR : {0}", wp_rsidRAttrib);
                            }
                            if (wp_rsidRDefaultAttrib != null)
                            {
                                Console.WriteLine("w:p : w:rsidRDefault : {0}", wp_rsidRDefaultAttrib);
                            }
                            if (wp_rsidPAttrib != null)
                            {
                                Console.WriteLine("w:p : w:rsidP : {0}", wp_rsidPAttrib);
                            }
                            if (wp_rsidRPrAttrib != null)
                            {
                                Console.WriteLine("w:p : w:rsidRPr : {0}", wp_rsidRPrAttrib);
                            }
                            if (wp_rsidTrAttrib != null)
                            {
                                Console.WriteLine("w:p : w:rsidTr : {0}", wp_rsidTrAttrib);
                            }
                            break;

                        case "w:r":

                            string wr_rsidRAttrib = reader["w:rsidR"];
                            string wr_rsidRDefaultAttrib = reader["w:rsidRDefault"];
                            string wr_rsidPAttrib = reader["w:rsidP"];
                            string wr_rsidRPrAttrib = reader["w:rsidRPr"];
                            string wr_rsidTrAttrib = reader["w:rsidTr"];

                            if (wr_rsidRAttrib != null)
                            {
                                Console.WriteLine("w:r : w:rsidR : {0}", wr_rsidRAttrib);
                            }
                            if (wr_rsidRDefaultAttrib != null)
                            {
                                Console.WriteLine("w:r : w:rsidRDefault : {0}", wr_rsidRDefaultAttrib);
                            }
                            if (wr_rsidPAttrib != null)
                            {
                                Console.WriteLine("w:r : w:rsidP : {0}", wr_rsidPAttrib);
                            }
                            if (wr_rsidRPrAttrib != null)
                            {
                                Console.WriteLine("w:r : w:rsidRPr : {0}", wr_rsidRPrAttrib);
                            }
                            if (wr_rsidTrAttrib != null)
                            {
                                Console.WriteLine("w:r : w:rsidTr : {0}", wr_rsidTrAttrib);
                            }
                            break;

                        case "w:tr":

                            string wtr_rsidRAttrib = reader["w:rsidR"];
                            string wtr_rsidRDefaultAttrib = reader["w:rsidRDefault"];
                            string wtr_rsidPAttrib = reader["w:rsidP"];
                            string wtr_rsidRPrAttrib = reader["w:rsidRPr"];
                            string wtr_rsidTrAttrib = reader["w:rsidTr"];

                            if (wtr_rsidRAttrib != null)
                            {
                                Console.WriteLine("w:tr : w:rsidR : {0}", wtr_rsidRAttrib);
                            }
                            if (wtr_rsidRDefaultAttrib != null)
                            {
                                Console.WriteLine("w:tr : w:rsidRDefault : {0}", wtr_rsidRDefaultAttrib);
                            }
                            if (wtr_rsidPAttrib != null)
                            {
                                Console.WriteLine("w:tr : w:rsidP : {0}", wtr_rsidPAttrib);
                            }
                            if (wtr_rsidRPrAttrib != null)
                            {
                                Console.WriteLine("w:tr : w:rsidRPr : {0}", wtr_rsidRPrAttrib);
                            }
                            if (wtr_rsidTrAttrib != null)
                            {
                                Console.WriteLine("w:tr : w:rsidTr : {0}", wtr_rsidTrAttrib);
                            }
                            break;

                        case "w:sectPr":

                            string wsPr_rsidRAttrib = reader["w:rsidR"];
                            string wsPr_rsidRDefaultAttrib = reader["w:rsidRDefault"];
                            string wsPr_rsidPAttrib = reader["w:rsidP"];
                            string wsPr_rsidRPrAttrib = reader["w:rsidRPr"];
                            string wsPr_rsidTrAttrib = reader["w:rsidTr"];

                            if (wsPr_rsidRAttrib != null)
                            {
                                Console.WriteLine("w:sectPr : w:rsidR : {0}", wsPr_rsidRAttrib);
                            }
                            if (wsPr_rsidRDefaultAttrib != null)
                            {
                                Console.WriteLine("w:sectPr : w:rsidRDefault : {0}", wsPr_rsidRDefaultAttrib);
                            }
                            if (wsPr_rsidPAttrib != null)
                            {
                                Console.WriteLine("w:sectPr : w:rsidP : {0}", wsPr_rsidPAttrib);
                            }
                            if (wsPr_rsidRPrAttrib != null)
                            {
                                Console.WriteLine("w:sectPr : w:rsidRPr : {0}", wsPr_rsidRPrAttrib);
                            }
                            if (wsPr_rsidTrAttrib != null)
                            {
                                Console.WriteLine("w:sectPr : w:rsidTr : {0}", wsPr_rsidTrAttrib);
                            }
                            break;
                    }
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM