简体   繁体   English

使用命名空间获取和设置XML元素

[英]Get and Set XML elements with Namespaces

NEWBIE QUESTION. 新手问题。

I haven't worked that much with xml, nothing like this anyway. 我在xml方面工作不多,无论如何都没有。 I have some XML as shown below that I receive which has several namespaces. 我收到了一些如下所示的XML,其中包含几个名称空间。

I need to read some values, then update others before returning the revised XML with namespaces intact - don't want them removed. 我需要读取一些值,然后再更新其他值,然后返回具有完整名称空间的修订XML-不想删除它们。

I am given the path to some of the elements like this cred/sub/aa or trip/items/item[0]/customerInfo/custName . 我得到了一些诸如cred/sub/aatrip/items/item[0]/customerInfo/custName类的元素的路径。

But it seems that namespaces make it difficult to get to those elements so simply. 但是似乎命名空间使很难简单地访问这些元素。

Does anybody know how I can read some of the values like NON-SMOKING from custPref or get the value CABBAGE from bossman/zz . 有谁知道我可以读一些类似的值的NON-SMOKINGcustPref或获得的价值CABBAGEbossman/zz

Also, I want to be able to then set a value such as custName to say Mr. X . 另外,我希望能够设置诸如custName的值来表示Mr. X

Any ideas? 有任何想法吗?

Thanks. 谢谢。

<?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