簡體   English   中英

使用命名空間獲取和設置XML元素

[英]Get and Set XML elements with Namespaces

新手問題。

我在xml方面工作不多,無論如何都沒有。 我收到了一些如下所示的XML,其中包含幾個名稱空間。

我需要讀取一些值,然后再更新其他值,然后返回具有完整名稱空間的修訂XML-不想刪除它們。

我得到了一些諸如cred/sub/aatrip/items/item[0]/customerInfo/custName類的元素的路徑。

但是似乎命名空間使很難簡單地訪問這些元素。

有誰知道我可以讀一些類似的值的NON-SMOKINGcustPref或獲得的價值CABBAGEbossman/zz

另外,我希望能夠設置諸如custName的值來表示Mr. X

有任何想法嗎?

謝謝。

<?xml version="1.0" encoding="utf-16" ?> 
<A1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <cred xmlns="https://blah-blah.com/?foobar">
        <sub>
            <aa>Zippo</aa> 
            <bb>lighter</bb> 
        </sub>
        <reqId>
            <cc></cc> 
            <dateOfBirth></dateOfBirth> 
        </reqId>
    </cred>
    <reqName xmlns="http://blah-blah/vader/base">qwerty</reqName> 
    <reqId xmlns="http://blah-blah/vader/base">12345</reqId> 
    <machine xmlns="http://blah-blah/vader/base">
        <qqq>hello</qqq> 
        <www>goodbye</www> 
        <eee>99999</eee> 
        <rrr>88888</rrr> 
    </machine>
    <monkey xmlns="http://blah-blah/vader/base">alskdjfhg</monkey> 
    <math xmlns="http://blah-blah/vader/base">
        <language></language> 
    </math>
    <trip xmlns="http://blah-blah/simple">
        <tripOverview xmlns="http://blah-blah/vader/base">
            <description></description> 
            <cost></cost> 
        </tripOverview>
        <bossman xmlns="http://blah-blah/vader/base">
            <zz>CABBAGE</zz> 
            <yy>BANANA</yy> 
            <xx>MELON</xx> 
            <ww>SYRUP</ww> 
        </bossman>
        <items>
            <item>
                <itemSummary xmlns="http://blah-blah/vader/base">
                    <description></description> 
                    <cost></cost> 
                    <reference></reference> 
                </itemSummary>
                <customerInfo xmlns="http://blah-blah/vader/base">
                    <custName></custName> 
                    <custPref>NON-SMOKING</custPref>
                </customerInfo>
                <seatId xmlns="http://blah-blah/vader/base">1</seatId> 
            </item>
        </items>
    </trip>
  </A1>
string xml = "<Root><Options></Options></Root>";

var xdocs = XDocument.Parse(xml);

xdocs.Descendants().Where(q => q.Name == "Options").FirstOrDefault().Value = "FoundIt";

暫無
暫無

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

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