gpt4 book ai didi

c# - 使用 Linq to XML 创建响应 xml 的命名空间问题

转载 作者:行者123 更新时间:2023-11-30 12:47:23 24 4
gpt4 key购买 nike

我是 L2XML 的新手,并不是 XML 的专家,所以我遇到一些小麻烦也就不足为奇了。在我的第一步中,我声明了一个相对简单的 XDocument 对象来创建 XML 方法结果。

这是预期 XML 的示例。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Body>
<TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ACORD.org/Standards/Life/2 TXLife2.28enum.XSD">
<UserAuthResponse>
<TransResult>
<ResultCode tc="1">Success</ResultCode>
</TransResult>
<SvrDate>2010-12-02</SvrDate>
<SvrTime>14:40:50-06:00</SvrTime>
</UserAuthResponse>
<TXLifeResponse>
<TransRefGUID>V7504892456123812</TransRefGUID>
<TransType tc="121">General Requirement Order Request</TransType>
<TransExeDate>2010-12-02</TransExeDate>
<TransExeTime>14:40:50-06:00</TransExeTime>
<TransMode tc="2">Original</TransMode>
<TestIndicator tc="1">Yes</TestIndicator>
<TransResult>
<ResultCode tc="1">Success</ResultCode>
</TransResult>
<OLifE>
<SourceInfo>
<CreationDate>2010-12-02</CreationDate>
<CreationTime>14:40:50-06:00</CreationTime>
<SourceInfoName>External Vendor Name</SourceInfoName>
</SourceInfo>
</OLifE>
</TXLifeResponse>
</TXLife>
</soap:Body>
</soap:Envelope>

这是我用来尝试创建与上述匹配的内容的代码:

public string SubmitOrder121(string xmlIn)
{
string resultText = "SUCCESS"; //Hard coded for now. Needs to be set based on result of call to CrossBow.
string resultCode = "1"; //Same comment as above.
string date = DateTime.Today.ToShortDateString();
string time = DateTime.Now.ToShortTimeString();
string transRefGUID = "V7504892456123812"; //Hard coded for now. Get from xmlIn;
string transModeText = "Original"; //Don't know what this is for or where to get it if there are other possibilities
string transModeCode = "2"; //Same as above comment
string testIndicatorText = "True"; //Get from config file
string testIndicatorCode = "1"; //Get from config file
string companyName = "External Vendor Name"; //Get from config file

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace soap = "http://www.w3.org/2001/12/soap-envelope";
XNamespace xmlns = "http://ACORD.org/Standards/Life/2";

XDocument xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XElement(soap + "Envelope",
new XAttribute(XNamespace.Xmlns + "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"),
new XAttribute(XNamespace.Xmlns + "wsa", "http://schemas.xmlsoap.org/ws/2004/03/addressing"),
new XAttribute(XNamespace.Xmlns + "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"),
new XAttribute(XNamespace.Xmlns + "soap", "http://schemas.xmlsoap.org/soap/envelope/"),
new XElement(soap + "Body",
new XElement(xmlns + "TXLife",
new XAttribute(xsi + "schemaLocation", "http://ACORD.org/Standards"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XElement("UserAuthResponse",
new XElement("TransResult",
new XElement("ResultCode", resultText,
new XAttribute("tc", resultCode)
)
),
new XElement("SvrDate", date),
new XElement("SvrTime", time)
),
new XElement("TXLifeResponse",
new XElement("TransRefGUID", transRefGUID),
new XElement("TransType", "General Requiremeent Order Request",
new XAttribute("tc", "121")
),
new XElement("TransExeDate", date), //Get from crossbow result
new XElement("TransExeTime", time), //Get from crossbow result
new XElement("TransMode", transModeText,
new XAttribute("tc", transModeCode)
),
new XElement("TestIndicator", testIndicatorText,
new XAttribute("tc", testIndicatorCode)
),
new XElement("TransResult",
new XElement("ResultCode", resultText,
new XAttribute("tc", resultCode)
)
),
new XElement("OLife",
new XElement("SourceInfo",
new XElement("CreationDate", date),
new XElement("CreationTime", time),
new XElement("SourceInfoName", companyName)
)
)
)
)
)
)
);
return xdoc.ToString();
}

现在,考虑到我到目前为止所能理解的一点点,上面的内容应该能给我我想要的,但它并没有——完全正确。它给了我这个:

<Envelope xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.w3.org/2001/12/soap-envelope">
<Body>
<TXLife xsi:schemaLocation="http://ACORD.org/Standards" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ACORD.org/Standards/Life/2">
<UserAuthResponse xmlns="">
<TransResult>
<ResultCode tc="1">SUCCESS</ResultCode>
</TransResult>
<SvrDate>6/14/2013</SvrDate>
<SvrTime>1:57 PM</SvrTime>
</UserAuthResponse>
<TXLifeResponse xmlns="">
<TransRefGUID>V7504892456123812</TransRefGUID>
<TransType tc="121">General Requiremeent Order Request</TransType>
<TransExeDate>6/14/2013</TransExeDate>
<TransExeTime>1:57 PM</TransExeTime>
<TransMode tc="2">Original</TransMode>
<TestIndicator tc="1">True</TestIndicator>
<TransResult>
<ResultCode tc="1">SUCCESS</ResultCode>
</TransResult>
<OLife>
<SourceInfo>
<CreationDate>6/14/2013</CreationDate>
<CreationTime>1:57 PM</CreationTime>
<SourceInfoName>The Company Name</SourceInfoName>
</SourceInfo>
</OLife>
</TXLifeResponse>
</TXLife>
</Body>
</Envelope>

忽略日期和时间格式。我知道它们不匹配,但这是我稍后会关心的事情,就像我会关心硬编码值一样。我更关心 XML 格式,尤其是以下内容:

  1. 为什么 XDeclaration 没有出现在 XML 结果中?
  2. 在 Soap 信封和主体上,为什么没有出现 soap 前缀?
  3. 如何抑制 TXLifeResponse> 标签上的 xmlns 属性?

我已经在引用此链接的其他类似问题的答案中看到了引用资料: http://msdn.microsoft.com/en-us/library/bb387042.aspx但这对我帮助不大。

最佳答案

  1. ToString() 方法从不发出 XML 声明。原因是不同的故事,但请检查 xdoc.Save("sample.xml");写声明。

  2. XNamespace soap = "http://www.w3.org/2001/12/soap-envelope"; 行,您可能有错字,将其更改为 http://schemas.xmlsoap.org/soap/envelope/

  3. 您必须为 TXLife 元素的所有子元素指定默认命名空间,如下所示:

    new XElement(xmlns + "UserAuthResponse",
    new XElement(xmlns + "TransResult",
    new XElement(xmlns + "ResultCode", resultText,
    new XAttribute("tc", resultCode)

希望对你有帮助

关于c# - 使用 Linq to XML 创建响应 xml 的命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17117254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com